I''ve been using rails for about 3 months and have a pretty cool app going, but i need to auto-fill some form data. is this possible? i would think so. well anybody to lend me a helping hand, much appreciated. -- Posted via http://www.ruby-forum.com/.
If you are using a form based on a model you can use the model to set default values. class Person < ActiveRecord::Base alias :old_initialize :initialize def initialize(attributes = nil) old_initialize(attributes) self.country = "New Zealand" if country.blank? end end Something like that should work. There may be a better way though. If the form is not based on a model, just pass values in with the calls to the form helpers. -Jonathan. On 3/14/06, Jon Mr <jon.druse@gmail.com> wrote:> > I''ve been using rails for about 3 months and have a pretty cool app > going, but i need to auto-fill some form data. is this possible? i would > think so. well anybody to lend me a helping hand, much appreciated. >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/785246cb/attachment-0001.html
what if i want to fill it with data from another table? and yes the form is based on a model. thanks, that info was very helpful... -- Posted via http://www.ruby-forum.com/.
Use the example I gave you, but find the other model and use whatever fields you want. Perhaps something like... class Person < ActiveRecord::Base alias :old_initialize :initialize def initialize(attributes = nil) old_initialize(attributes) defaults = PersonDefaults.find(:first) self.country = defaults.country if country.blank? end end -Jonathan. On 3/14/06, Jon Mr <jon.druse@gmail.com> wrote:> > what if i want to fill it with data from another table? and yes the > form is based on a model. thanks, that info was very helpful... >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/99bd6aab/attachment.html
On Monday 13 Mar 2006 22:32, Jon Mr wrote:> what if i want to fill it with data from another table? and yes the > form is based on a model. thanks, that info was very helpful...Controller: @otherdata = Otherdata.find(:all, :order => "name").map {|d| [d.name, d.id]} View: <%= select("mainmodel", "otherdata_id", @otherdata) %> That''s it! ;-) ~Dave -- Dave Silvester Rent-A-Monkey Website Development http://www.rentamonkey.com/ PGP Key: http://www.rentamonkey.com/pgpkey.asc
That''s another approach. The reason I suggested putting it down on the model is that it will give default values for any form using that model. Putting it in the controller will mean you need to repeat yourself to reuse the default values. And, to me at least, this seems like an issue which should be sorted out at the model level, not with controllers/views. But that is just my opinion, feel free to disagree. -Jonathan. On 3/14/06, Dave Silvester <dave@rentamonkey.com> wrote:> > On Monday 13 Mar 2006 22:32, Jon Mr wrote: > > what if i want to fill it with data from another table? and yes the > > form is based on a model. thanks, that info was very helpful... > > Controller: > > @otherdata = Otherdata.find(:all, :order => "name").map {|d| [d.name, d.id > ]} > > View: > > <%= select("mainmodel", "otherdata_id", @otherdata) %> > > That''s it! ;-) > > ~Dave >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/fc486261/attachment.html
On Monday 13 Mar 2006 22:59, Jonathan Viney wrote:> That''s another approach. The reason I suggested putting it down on the > model is that it will give default values for any form using that model. > Putting it in the controller will mean you need to repeat yourself to reuse > the default values. > > And, to me at least, this seems like an issue which should be sorted out at > the model level, not with controllers/views. But that is just my opinion, > feel free to disagree.Sure, I often move that kind of thing out into the model in it''s own function, something like: Controller: @otherdata = Otherdata.find_for_select Model: def self.find_for_select find(:all, :order => "name").map {|d| [d.name, d.id]} end But actually, your approach is pretty nice and I can see what you''re trying to do (set it up by default rather than having to expressly call it) - though I''m not sure it really answers his question since it seems to me that he''s probably asking how to populate select lists with data from another table. I don''t know!? Jon Mr we could do with a bit more detail and a better description of exactly what you''re trying to do. What kind of field are you trying to populate? Jonathan Viney - I''m not sure I can work out how your method could be used to generate a select field, though I can see how it could automatically be used to populate the data for that field. I think you''re always going to need something in your view, surely? Though moving it into the model on initialize is a good way to get that data in the first place - I''ll definitely do it that way from now on, as it seems more elegant. So ultimately, perhaps Jon Mr requires a combination of our two methods? ~Dave -- Dave Silvester Rent-A-Monkey Website Development http://www.rentamonkey.com/ PGP Key: http://www.rentamonkey.com/pgpkey.asc