Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
STI form params name
3 Posts
STI form params name

STI saved me from a problem but created another one. Lets say I have this entirely contrived model...

class Beast : ActiveRecord::Base end

class SeaBeast < Beast end

class LandBeast < Beast

validates_presence_of :number_of_legs end

Since my form only differs by the one field number_of_legs, I created one form to keep things DRY. What I did not expect was that the object class name gets used as the params[] name and the field names.

In the case of LandBeast the fields start out as land_beast_ and the object posted back is params[:land_beast].

Like wise for SeaBeast: sea_beast and params[:sea_beast]. If I use the parent object then it is beast and params[:beast].


Can this name be overridden? By the way I am using a form_for, not form_tag.

Further side complication.

One of the gems I am using for date time selection uses the parent class name, so no matter what gets passed to it, it uses beast_ and params[:beast]. So every post I am getting two different hashes that I now have to combine so I can do one save or update_attributes.

One step closer.

I realized I should have been using the form_form like this

form_for :beast, some_beast.

However, this breaks the routing and I cannot seem to get it back in line. I have tried different combinations of :url and :method but not matter what it wants to do a post when it should be doing a put. Of course it bombs out because I do not have a create method, because it should be doing an edit action.

I hope someone benefits from this, it was a struggle for me. The proper form_for looks like this and the key was that :method had to go inside the :html hash.

form_for :beast, some_beast, :url => beast_path, :html => { :method => "put" }

3 Posts
Login to add your message