68979

Relationships controller for creating an ajax relationship -NoMethodError: undefined method `[model]

Question:

I believe I am having problems with the redirect_to in my controller below. A course has many levels, which has many steps (step belongs to level, and level belongs to course). I am trying to redirect_to the step.

Here is my route for a step:

/courses/:course_id/levels/:level_id/steps/:id(.:format)

Here is my error in the controller:

Failure/Error: click_button "check answer" NoMethodError: undefined method `step_url' for #<UserStepsController:0x007ff8ccc478c8>

Here is the controller in which the error occurs:

class UserStepsController < ApplicationController before_filter :authenticate_user! def create @step = Step.find(params[:user_step][:step_id]) current_user.attempt_step!(@step) respond_to do |format| format.html { redirect_to @step } format.js end end def destroy @step = UserStep.find(params[:id]).step current_user.remove_user_step!(@step) respond_to do |format| format.html { redirect_to @step } format.js end end end

Here is the rspec

describe "attempting a step" do let(:course) { FactoryGirl.create(:course)} let(:level) { FactoryGirl.create(:level, course: course) } let(:step) { FactoryGirl.create(:step, level: level) } before { sign_in user} describe "taking a course" do before { visit course_level_step_path(course.id, level.id, step.id)} it "should increment the user step count" do expect do click_button "check answer" end.to change(user.user_steps, :count).by(1) end describe "toggling the button" do before { click_button "check answer"} it { should have_selector('input', value: 'remove step')} end end

Answer1:

You can't use redirect_to @variable when you have nested resources. The correct way would be for you to say

redirect_to course_level_step_url(@step.level.course.id, @step.level.id, @step.id)

Because the thing that gets the route from the instance variable 'thinks' you want the path of the lowercased class name plus _url

Recommend

  • Before filter in action mailer Rails 3
  • installing rails with gem , error loading command : install undefined method 'invoke_with_build
  • Method referring to a pulled in packaged's method moves out of scope when put inside an objects
  • Set read_timeout for the service call in Ruby Net::HTTP.start
  • NoMethodError when I try to acces a field of an object taken from the DB
  • undefined method `paginate' for Array on Rails 3 with mongoid
  • Using the ruby-trello gem, how can I determine whether a board is public or not?
  • Why I'm getting undefined method `cookies'?
  • Writing a pre-push hook in Git to grep all files for regex want to reject push if regex not found
  • Cannot include module in model
  • Rails 3.1 limit the number of child models
  • NoMethodError on Rails Server
  • Mongoid - limit on distinct query
  • Ruby HTTP POST - Errors
  • NoMethodError - undefined method `timestamp_sort_order' for Paper trail issue after upgrading R
  • Adding numbers in succession
  • Ruby on Rails , Rspec Error while testing
  • Sorting a hash based on array of order in ruby
  • NoMethodError: undefined method `call'
  • Error while using PDF Kit
  • runtime error when linking ffmpeg libraries in qt creator
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Angular2 - Template reference inside NgSwitch
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • MongoDb aggregation
  • Is there a way to do normal logging with EureakLog?
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal