Forcing link_to 'put'

I cannot force my link_to statement to send an HTTP put into my controller's update method. It appears to be using get and therefore invoking the show method. Here is what my view looks like (I am using nested resources where item :has_many :costs):

<%= link_to 'Recalculate', item_cost_path(:recalc => 1), :method => :put, :action => :update %>

When I click on the link in a browser:

Unknown action No action responded to show. Actions: create, destroy, edit, index, new, and update

I have tried many variations of link_to arguments and have been unable to make this work. Out of ideas, so I'm hoping you can help.

Thanks! Mark

What happens if you remove ":action => :update"? Shouldn't be necessary cause put automatically calls the update method.

Unfortunately that does not work. Same error, as if link_to is ignoring the :method argument and defaulting to get/show.

Unfortunately that does not work. Same error, as if link_to is ignoring the :method argument and defaulting to get/show.

Have you checked the html generated by the link, and looked in the log to see if anything useful there?

Colin

Colin Law wrote:

Have you checked the html generated by the link, and looked in the log to see if anything useful there?

Colin

Yeah, what's the output of <%= link_to 'Recalculate', item_cost_path(:recalc => 1), :method => :put %>?

What have you got?

Colin

btw, found not much time ago, that you need to do

:conditions => {:method => :put}

instead of just

:method => :put

in routes.rb when define a route

The answer to seemingly broken rails behavior is usually "duh..I should have realized that!"

My link_to syntax was fine. The HTTP put was successfully finding the correct controller method, but that method was performing a redirect that was failing (in a rather opaque way to me) because in this particular code path I did not initialize an instance variable (@item) that was the parameter to the redirect because I was bypassing a form_for...submit action that otherwise passes in the item.id:

    redirect_to edit_item_cost_path(@item, @cost)

It appears that this RESTful helper fails with a not-too-insightful error when passed a bogus parameter.

Thanks everyone who pitched in with suggestions and pointers.

Mark