Update field through link_to in Rails

ruby, ruby-on-rails, ruby-on-rails-3

Solution

Try

<%= link_to "Remove", update_products_path(product:{parent_id: nil}), method: :put %>

There is no HTTP-Verb `update` what you need is `put`. You can read about Rails and HTTP-Verbs here

Problem

I have a tree of products in Rails 3.2 and would like to have an add/remove feature so that users can add new children products or remove existing children for a product. I use the ancestry gem to generate the tree, for a Product 1 it might look like this: ``` Product 1 add | remove Product 2 add | remove Product 3 add | remove ``` In a partial _product.html.erb I have added Add and Remove links, which works for the add feature, but I cannot get the remove link to work: ``` <span><%= product.name.capitalize %></span> <%= link_to "Add", new_product_path(parent_id: product) %> | <%= link_to "Remove", {parent_id: nil}, method: :update %> ``` I would like to update the parent_id to nil for the product to be removed when "Remove" is clicked, but the above link_to doesn't seem to be working. I get an: No route matches [POST] "/products/1/edit" routing error. In my product_controller I have: ``` def update if @product.update_attributes(params[:product]) flash[:success] = "Product updated" redirect_to @product else render 'edit' end end ``` What am I doing wrong? Edit: I tried to use `method: put` instead: ``` <%= link_to "Remove", {parent_id: nil}, method: :put %> ``` then I get a `No route matches [PUT] "/products/1/edit"` error instead when clicking on the link. I can now change/remove the parent using a form, not at all what I want but anyway: ``` <%= form_for @product do |f| %> <%= f.label :parent_id %> <%= f.text_field :parent_id %> <%= f.submit "Update", class: "btn" %> <% end %> ``` Would it be possible to automatically pass the parent_id: nil into the form so that when you hit Update, it sets the parent_id: nil (not having a text field just a button)?

Original source