开发者

Adding routes for actions that operate on one record in rails

I want to create a method in my rails app that will increase a value tied to a record.

The method in the controller looks like this:

def upvote
  @spot = Spot.find(params[:id])
  @spot.rating += 1

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @spot }
  end
end

Then when viewing the record at "spots/1" I include this code:

<%= link_to 'Upvote', @spot, :confirm => 开发者_StackOverflow中文版'Are you sure?', :method => :upvote %>

Which, when clicked, throws the error:

"No route matches "/spots/1""

Even though I'm already at /spots/1. I know this is a routes issue, but I can't seem to give this method a route that works...


The parameter :method identifies the HTTP verb (GET, POST, UPDATE, ...), not the method to be called.

To add a new action, edit the routes.rb file

resources :sposts do
  member do
    put :upvote
  end
end

Then use a named route

<%= link_to 'Upvote', upvote_spot_path(@spot), :confirm => 'Are you sure?', :method => :put %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜