Arel
Arel

Reputation: 3938

RSpec controller spec unable to update record

I have a Rails/RSpec controller spec that is throwing the error:

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'

The line put :update, {id: completed_set.to_param, completed_set: valid_attributes} is where it's blowing up. I don't know what the completed_set_url method is, or why/where it is being called.

The entire spec is below:

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

For completeness, here is my controller code:

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

Upvotes: 0

Views: 112

Answers (1)

theunraveler
theunraveler

Reputation: 3284

It's being called from redirect_to @completed_set in your update action.

The issue here is that your update action redirects to @completed_set (completed_set_url) when it's successful, but your app doesn't define a show action for that resource.

So, in your config/routes.rb file, define a show action (maybe using resources :completed_set, if your controller is RESTful), then define a show method in your controller, or change the redirect_to to be the same as your create action (redirect_to profile_path).

Upvotes: 1

Related Questions