用于创建ajax关系的关系控制器-NoMethodError:未定义方法`[model] _url' [英] Relationships controller for creating an ajax relationship -NoMethodError: undefined method `[model]_url'
问题描述
我相信下面的控制器中的redirect_to存在问题.一门课程有许多级别,其中有很多步骤(步骤属于级别,而级别属于课程).我正在尝试将其重定向到该步骤.
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.
这是我要走的路:
/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
这是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
推荐答案
当您嵌套资源时,不能使用redirect_to @variable.正确的方法是让你说
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)
因为要从实例变量"thinks"获取路由,所以您需要使用小写的类名加上_url的路径
Because the thing that gets the route from the instance variable 'thinks' you want the path of the lowercased class name plus _url
这篇关于用于创建ajax关系的关系控制器-NoMethodError:未定义方法`[model] _url'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!