Rails 3 Problem with routes
I have a haml link that looks like this
=link_to "accept", friendship, :method => :put
and i am trying to map "put" to use the "update" action in my controller
in my routes like so
resources :friendships do
collection do
get :create
delete :destroy
put :update
开发者_高级运维 end
end
so if i use method "get" it should get a new friend (that works), if i delete then the "destroy" action is used and if i put the "update" action gets used.
I take it I am doing this completely wrong somehow.
my "create" friend link looks like this
=link_to "Add Friend", friendships_path(:friend_id => provider), :method => :post
and this is my whole friends.haml
.profile
.providers
%h1 Other Users
- @providers.each do |provider|
%p
=provider.login
=link_to "Add Friend", friendships_path(:friend_id => provider), :method =>
:post
.friends
%h1 Friends
- @friends.each do |friendship|
%p
=friendship.friend.login
=link_to "remove", friendship, :method => :delete
=link_to "message", memos_path(:other_user => friendship.friend)
.friends-out
%h1 Friends Out
- @friends_out.each do |friendship|
%p
=friendship.friend.login
=link_to "remove", friendship, :method => :delete
.friends-in
%h1 Friends In
- @friends_in.each do |friendship|
%p
=friendship.friend.login
=link_to "remove", friendship, :method => :delete
=link_to "accept", friendship, :method => :put
Yeah, you're almost doing right, but your doing it the wrong way. You should do like this:
resources :friendships
No more, no less, that's it. Because when you use resources, it will by default map the put protocol to the update action, it will map the delete protocol to the destroy action, and it will map the POST protocol to the create action. Because you should not use GET when the database is somehow changed (create, update, destroy).
And when you use the link_to helper, try it like this:
=link_to "accept", friendship_path(@friendship), :method => :put
Edit:
Also make sure you have the default javascript included to handle the specified method of posting:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
精彩评论