RSpec控制器规范无法更新记录

时间:2015-02-12 16:19:41

标签: ruby-on-rails ruby rspec controller

我有一个抛出错误的Rails / RSpec控制器规范:

NoMethodError: undefined method `completed_set_url' for #<CompletedSetsController:0x007fcfe935c5d0>
from /Users/arelenglish/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0/lib/action_dispatch/routing/polymorphic_routes.rb:268:in `handle_model_call'

线put :update, {id: completed_set.to_param, completed_set: valid_attributes}正在爆炸。我不知道completed_set_url方法是什么,或者为什么/在哪里调用它。

整个规范如下:

it "assigns the requested completed_set as @completed_set" do
  completed_set = CompletedSet.create! valid_attributes
  put :update, {id: completed_set.to_param, completed_set: valid_attributes}
  expect(assigns(:completed_set)).to eq(completed_set)
end

为了完整性,这是我的控制器代码:

class CompletedSetsController < BaseController
  before_action :set_completed_set, only: [:edit, :update, :destroy]

  # POST /completed_sets
  def create
    @completed_set = CompletedSet.new(completed_set_params)
    if @completed_set.save
      redirect_to profile_path, notice: 'Successfully completed set.'
    else
      raise "Failed to save because #{@completed_set.errors.each {|e| e}}"
    end
  end

  # PUT /completed_sets/1
  def update
    respond_to do |format|
      if @completed_set.update_attributes(completed_set_params)
        format.html { redirect_to @completed_set, notice: 'Completed set was successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

  private
    # User callbacks to share common setup or constraints between actions.
    def set_completed_set
      @completed_set = CompletedSet.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def completed_set_params
      params.require(:completed_set).permit(
        :repetitions, 
        :set_number, 
        :user_id, 
        :weight, 
        :user_program_id, 
        :rpe, 
        :workout_exercise_id,
        :workout_exercise_set_id 
        )
    end

end

1 个答案:

答案 0 :(得分:1)

redirect_to @completed_set行动中从update拨打电话。

此处的问题是,update操作成功后会重定向到@completed_setcompleted_set_url),但您的应用并未定义{{1}对该资源的操作。

因此,在您的show文件中,定义一个show动作(如果您的控制器是RESTful,可能使用config/routes.rb),然后在控制器中定义resources :completed_set方法,或者更改showredirect_to操作(create)相同。