我在views / questions / show.html.erb中有一个链接,可以让用户删除标签。
<%=link_to "x",
:remote => true,
:url => remove_question_tag_path(@question, tag),
:method => :delete,
:html => { :id => "delete-#{tag.name.parameterize}"} %>
<% end %>
remove_question_tag_path路由是通过将tags资源嵌套在questions资源中创建的。
resources :questions do
resources :answers do
member { post :vote }
end
resources :tags do
member do
delete :remove
end
end
end
Rake路由显示此路由存在,因为我尝试在URL中使用它
remove_question_tag DELETE /questions/:question_id/tags/:id/remove(.:format) tags#remove
然而,当我点击链接时,它正在向Questions控制器的show动作发出get请求,而不是Tags控制器的remove动作,因为rake routes指示是路由的目标。
Started GET "/questions/25?html%5Bid%5D=delete-outdoors&method=delete&url=%2Fquestions%2F25%2Ftags%2F2%2Fremove" for 127.0.0.1 at 2013-03-26 19:01:00 -0700
你能解释一下我可能做错了什么吗?
答案 0 :(得分:1)
试试这个:
<%= link_to "x", remove_question_tag_path(@question, tag), :remote => true, :method => :delete, :html => { :id => "delete-#{tag.name.parameterize}"} %>
说明:您没有为链接指定url,因此link_to
会对除"x"
之外的所有给定参数进行哈希处理,并将它们视为url选项。因此,:method
选项仅添加到GET
参数,而不是生成DELETE
请求。