James
James

Reputation: 5777

Why does my route insist on being GET, when it ~should~ be PUT

I have this link in an index view.

<%= link_to "Accept!", playtime_accept_path(playtime) unless playtime.accepted? %>

The purpose of this link is to update a Playtime record that already exists, upon creation it's :accepted attribute is defaulted to false, and users click this link to change that.

The controller action is extremely simple (although if there is an even simpler way to do this I'd love to know)

  def accept
    @playtime = Playtime.find(params[:playtime_id])    
    @playtime.accepted = true

    respond_to do |format|
      if @playtime.update_attributes(params[:playtime])
        format.html { redirect_to @playtime, notice: 'Playtime was successfully accepted.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

Finally I'm using this route to make all the magic happen

resources :playtimes do
  get 'accept' => "playtimes#accept", :as => :accept 
end

Shouldn't this be a PUT request? If so, what's the best way to make that happen?

Upvotes: 1

Views: 41

Answers (1)

jvnill
jvnill

Reputation: 29599

Update your routes to tell Rails that the action can only be accessed via put requests

resources :playtimes do
  put :accept, on: :member
end

Then update the link to

<%= link_to "Accept!", accept_playtime_path(playtime), method: :put unless playtime.accepted? %>

UPDATE

I forgot to mention that instead of params[:playtime_id], you need to use params[:id]

@playtime = Playtime.find(params[:id])

Upvotes: 3

Related Questions