Forum: Ruby on Rails calling an actionview method from inside a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-03-16 02:44
(Received via mailing list)
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
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-03-16 02:56
(Received via mailing list)
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
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-03-16 03:24
(Received via mailing list)
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
5321dd2dc03ca82c1fed417cb1c94b95?d=identicon&s=25 Steven Beales (Guest)
on 2006-03-16 03:48
(Received via mailing list)
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
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-03-16 03:54
(Received via mailing list)
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
This topic is locked and can not be replied to.