2013-03-27 17 views
1

我在views/questions/show.html.erb中有一個鏈接,它允許用戶刪除標籤。鏈接使得GET(而不是DELETE)請求到錯誤的控制器

<%=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 

耙路線表明,這條路存在的,因爲我嘗試在URL中使用它

remove_question_tag DELETE /questions/:question_id/tags/:id/remove(.:format)    tags#remove 

然而,當我點擊鏈接,它使一個GET請求的展示動作問題控制器,而不是標籤控制器的刪除操作,因爲耙路徑指示是路由的目的地。

Started GET "https://stackoverflow.com/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 

你能解釋我可能會做錯什麼嗎?

回答

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請求。

相關問題