用于创建ajax关系的关系控制器-NoMethodError:未定义的方法`[model] _url'

时间:2013-10-11 20:21:26

标签: ruby-on-rails ruby ajax rspec

我相信我的控制器中的redirect_to出现问题。课程有很多级别,有很多步骤(步骤属于级别,级别属于课程)。我正在尝试重定向到该步骤。

以下是我的步骤路线:

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

这是我在控制器中的错误:

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

以下是发生错误的控制器:

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

1 个答案:

答案 0 :(得分:0)

当您拥有嵌套资源时,不能使用redirect_to @variable。正确的方法是你说

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

因为从实例变量获取路径的东西'认为'你想要小写类名的路径加上_url