Leahcim
Leahcim

Reputation: 41919

link making GET (rather than DELETE) request to wrong controller

I have a link in views/questions/show.html.erb that lets users delete tags.

 <%=link_to "x",
:remote => true,
:url => remove_question_tag_path(@question, tag),
:method => :delete,
:html => { :id => "delete-#{tag.name.parameterize}"} %>

<% end %> 

The remove_question_tag_path route is created by nesting the tags resource inside the questions resource.

 resources :questions do 
    resources :answers do 
      member { post :vote }
      end
      resources :tags do
          member do 
            delete :remove
          end 
      end
  end 

Rake routes shows that this route exists as I try to use it in the url

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

However, when I click on the link, it's making a get request to the show action of the Questions controller, rather than the remove action of the Tags controller, as rake routes indicates is the destination for the route.

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

Can you explain what I might be doing wrong?

Upvotes: 1

Views: 82

Answers (1)

Ilia Khokhriakov
Ilia Khokhriakov

Reputation: 3687

Try this:

<%= link_to "x", remove_question_tag_path(@question, tag), :remote => true, :method => :delete, :html => { :id => "delete-#{tag.name.parameterize}"} %>

Explanation: you do not specify url for link so link_to makes a hash of all given arguments except "x" and treats them as url options. Therefore, :method option is just added to GET parameters instead of generating DELETE request.

Upvotes: 1

Related Questions