0

我相信我在下面的控制器中遇到了 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
4

1 回答 1

0

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

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

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

于 2013-10-11T21:08:27.787 回答