Browse the Ruby on Rails Community.

You are here: Forums General Discussion STI form params name...

Replytotopic

STI form params name

Posted in Forums : General Discussion

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

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].

THE QUESTION

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.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

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.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

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” }

Replytotopic

Other Recent Topics

General Discussion : Hanoi houses, apartments, villas and offices for lease

General Discussion : Sr. Backend ROR Engineer needed in Denver Metro Area.($120k)

General Discussion : Rails Developer($130k) needed in Denver, CO

General Discussion : House’ construction

General Discussion : Looking for full time Rails Web Developer

General Discussion : Free classified Directory (sadiqui999)

General Discussion : Louis Vuitton Damier Ebene brand Canvascheap special offer( www.salecheaplouisvuitton.com )

General Discussion : ROR Development

General Discussion : RoR programmers needed

General Discussion : Required Ruby on Rails Developer

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel