Peter Michaux
2006-Mar-16 01:43 UTC
[Rails] calling an actionview method from inside a model
Hi, I would like my model instance to produce it''s own list of options for a form select. This is from a product model that has_many variations def alts_for_select(current_id) the_map = variations.map{|v| [v.name, v.id]} options_for_select(the_map, current_id) end the model cannot see the options_for_select method I tried to use ActionView::Helpers::FormOptionsHelper::options_for_select(the_map, current_id) but this didn''t work. Any ideas? Thanks, Peter
Nick Stuart
2006-Mar-16 01:55 UTC
[Rails] calling an actionview method from inside a model
General consensus would say this is bad design for making your model tied to your view. The better way to do this would be to put a method in a helper and call it from there if you really don''t want to put the code in your view. -Nick On 3/15/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Hi, > > I would like my model instance to produce it''s own list of options for > a form select. This is from a product model that has_many variations > > def alts_for_select(current_id) > the_map = variations.map{|v| [v.name, v.id]} > options_for_select(the_map, current_id) > end > > the model cannot see the options_for_select method > > I tried to use > > ActionView::Helpers::FormOptionsHelper::options_for_select(the_map, > current_id) > > but this didn''t work. > > Any ideas? > > Thanks, > Peter > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/64abc78b/attachment-0001.html
Peter Michaux
2006-Mar-16 02:22 UTC
[Rails] calling an actionview method from inside a model
On 3/15/06, Nick Stuart <nicholas.stuart@gmail.com> wrote:> General consensus would say this is bad design for making your model tied to > your view. > > The better way to do this would be to put a method in a helper and call it > from there if you really don''t want to put the code in your view.OK, that sounds reasonable but here is more of my situation. I have an array of objects that I want to display in a view. Each object has to produce a drop down box. However, each object might be an instance of three different classes. Currently I have a if elsif elsif end structure that determines which class the object is from. Then the drop down box is created based on special rules for that drop down box. One class needs a drop down box that may or may not use grouped sections (option_groups_from_collection_for_select). I was hoping to move the logic to the model and then I would only need this in my view (or something simliar) <select> <% objs.each do |o| %> <%= o.alts_for_select(o.current_id) %> <% end %> </select> The elsif (or case) structure is gone which is kind of nice. ideas? Thanks, Peter
Steven Beales
2006-Mar-16 02:47 UTC
[Rails] Re: calling an actionview method from inside a model
In Model def alts_for_select variations.map{|v| [v.name, v.id]} end In View <% objs.each do |o| %> <%= options_for_select(o.alts_for_select, o.current_id) %> <% end %> Steven Beales "Peter Michaux" <petermichaux@gmail.com> wrote in message news:3cbaf1c80603151822l4c599766x656952fe77f7534@mail.gmail.com... On 3/15/06, Nick Stuart <nicholas.stuart@gmail.com> wrote:> General consensus would say this is bad design for making your model tied > to > your view. > > The better way to do this would be to put a method in a helper and call it > from there if you really don''t want to put the code in your view.OK, that sounds reasonable but here is more of my situation. I have an array of objects that I want to display in a view. Each object has to produce a drop down box. However, each object might be an instance of three different classes. Currently I have a if elsif elsif end structure that determines which class the object is from. Then the drop down box is created based on special rules for that drop down box. One class needs a drop down box that may or may not use grouped sections (option_groups_from_collection_for_select). I was hoping to move the logic to the model and then I would only need this in my view (or something simliar) <select> <% objs.each do |o| %> <%= o.alts_for_select(o.current_id) %> <% end %> </select> The elsif (or case) structure is gone which is kind of nice. ideas? Thanks, Peter
Peter Michaux
2006-Mar-16 02:54 UTC
[Rails] Re: calling an actionview method from inside a model
Hi Steven, After Nick''s replay I did think about this but about if one of objs for one of the classes has to produce a grouped select? Argg. Maybe it''s messy no matter what. Thanks, Peter On 3/15/06, Steven Beales <steven@mdlogix.com> wrote:> In Model > > def alts_for_select > variations.map{|v| [v.name, v.id]} > end > > In View > > <% objs.each do |o| %> > <%= options_for_select(o.alts_for_select, o.current_id) %> > <% end %> > > > Steven Beales > > "Peter Michaux" <petermichaux@gmail.com> wrote > in message > news:3cbaf1c80603151822l4c599766x656952fe77f7534@mail.gmail.com... > On 3/15/06, Nick Stuart > <nicholas.stuart@gmail.com> wrote: > > General consensus would say this is bad design for making your model tied > > to > > your view. > > > > The better way to do this would be to put a method in a helper and call it > > from there if you really don''t want to put the code in your view. > > > OK, that sounds reasonable but here is more of my situation. I have an > array of objects that I want to display in a view. Each object has to > produce a drop down box. However, each object might be an instance of > three different classes. Currently I have a if elsif elsif end > structure that determines which class the object is from. Then the > drop down box is created based on special rules for that drop down > box. One class needs a drop down box that may or may not use grouped > sections (option_groups_from_collection_for_select). I was hoping to > move the logic to the model and then I would only need this in my view > (or something simliar) > > <select> > <% objs.each do |o| %> > <%= o.alts_for_select(o.current_id) %> > <% end %> > </select> > > The elsif (or case) structure is gone which is kind of nice. > > ideas? > > Thanks, > > Peter > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >