Rails form_for routing issues

I have a form that I am using to try and update a field in "things". However, I am having serious problems that probably go down to something silly.

My code looks like this:

<% form_for :thing, :url => { :action => "update" } do |f| %>
    <%= f.collection_select :status_id, Thing.statuses, :first, :last %>
    <%= f.submit 'Submit' %>
<% end %>

      

This looks correct to me (@thing exists BTW), however, when I submit, I get

Unknown action
No action responded to 145

      

etc. etc. The URL is http: // localhost: 3000 / things / 145 . What am I doing wrong?

+1


a source to share


3 answers


If you've used RESTful routing (and I assume you did), you can simply do this:

<% form_for @thing do |f| %>
  <%= f.collection_select :status_id, Thing.statuses, :first, :last %>
  <%= f.submit 'Submit' %>
<% end %>

      



And this will indicate the correct action depending on whether it @thing

is new_record?

or not.

+4


a source


Using RESTful routes, the action is update

triggered when a request is recognized put

. You probably just need to specify the method as an html parameter:



form_for :thing, :url => { :action => "update" }, :html => { :method => 'put' } do |f|

      

+4


a source


I kind of despise ActionView so I am always rusty when it comes to it, but try

<% form_for :thing, :url => 
     url_for(:action => "update", :id => @thing) do |f| %>

      

I'm sure someone else can come up with something DRYer and cleaner.

+1


a source







All Articles