views:

697

answers:

3

I have a form that I am using to try and update a field on a 'thing', However, I'm getting massive problems that is probably down to something stupid.

My code is as follows:

<% 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 does exist BTW), however, when I submit I get

Unknown action
No action responded to 145

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

+1  A: 

I sort of despise ActionView so I'm 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.

kch
Any reason for the ActionView hate? ;)
Neil Middleton
Many. Could be an entirely separate SO question. But let's try to keep it small: erb sucks, I like haml; I'd prefer to use jQuery instead of Prototype, but helpers all generare Prototype code; I generally don't like the html output of the helpers anyway; It's dumb: view code is not aware of the view structure itself. For example I'd like a form to get it's multipart attribute set automatically when I add a file field. Can't do that. (Can't in haml either, but I have some amazing vapourware here that does this and much more. Hopefully one day it'll end up on github)
kch
I updated with something cleaner.
Ryan Bigg
+1  A: 

Using RESTful routes, the update action is triggered when a put request is recognized. You probably just need to specify the method as an html option:

form_for :thing, :url => { :action => "update" }, :html => { :method => 'put' } do |f|
Michael Sepcot
Not the restful way to do it, could do form_for @thing and it would work. Much tidier too.
Ryan Bigg
+3  A: 

If you were using RESTful routing (and I'm assuming you are) you could just do this:

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

And this will point to the correct action depending on if @thing is a new_record? or not.

Ryan Bigg