:Method =>删除路由不起作用

时间:2012-01-11 23:22:38

标签: ruby-on-rails ruby-on-rails-3 routes

这个删除路径的按钮似乎以某种方式路由到我的友谊#create动作:

<%= button_to "Unfriend", unfriend_path(@user), :method => :delete, :class => "btn primary", :remote => true %>

路线档案:

match 'friendships/:id', :to => 'friendships#create', :method => :post, :as => 'friendship_request'
match 'friendships/:id', :to => 'friendships#destroy', :method => :delete, :as => 'unfriend'

这是服务器日志

Started POST "/friendships/45" for 127.0.0.1 at 2012-01-11 19:56:46 -0500
  Processing by FriendshipsController#create as JS
  Parameters: {"authenticity_token"=>"UicOeyAtNuQTd1nAg8XiUtki7B5iOiDtPgJ/Wu8Z+i0=", "_"=>"", "method"=>:post, "id"=>"45"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 45 LIMIT 1
  Friendship Load (0.2ms)  SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = 101 AND "friendships"."friend_id" = 45 LIMIT 1
  Friendship Load (0.3ms)  SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = 45 AND "friendships"."friend_id" = 101 LIMIT 1
Rendered users/_invited.html.erb (0.3ms)
Rendered friendships/create.js.erb (1.5ms)

这是呈现的HTML(隐藏方法删除表单)

<form method="post" action="/friendships/45" data-remote="true" class="button_to"><div><input name="_method" type="hidden" value="delete"><input action="destroy" class="btn primary" type="submit" value="Unfriend"><input name="authenticity_token" type="hidden" value="UicOeyAtNuQTd1nAg8XiUtki7B5iOiDtPgJ/Wu8Z+i0="></div></form>

1 个答案:

答案 0 :(得分:0)

delete_to不支持删除选项,只有post和get。 (来源:http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

确保生成的表单中有一个名为_method的隐藏字段,其值为delete。

如果没有生成一个,您可以使用Javascript即时插入。