【问题标题】:link making GET (rather than DELETE) request to wrong controller将 GET(而不是 DELETE)请求链接到错误的控制器
【发布时间】:2013-03-17 01:22:51
【问题描述】:

我在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 路由是通过将标签资源嵌套在问题资源中来创建的。

 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

你能解释一下我可能做错了什么吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案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 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-20
      • 2011-01-09
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多