Search code examples
ruby-on-railslink-to

Using link_to to write a value to an attribute


I have a model for POs (purchase orders) and one of its attributes is status. I'm trying to write a link_to which will write the value "closed" to the status of the currently open PO.

show.html.erb

<%= link_to 'Close PO', {:action => :update, :method => :patch, :id => @po.id, :status => 'closed'} %>

This doesn't work so far, as the link brings me back to the PO show view and the status of the current PO remains "open". What am I doing wrong?


Solution

  • I would think your link_to would look more like:

    <%= link_to 'Close PO', po_path(@po, po: {status: :closed}), method: :patch %>
    

    This assumes, naturally, that your routes.rb includes:

    resources: :pos