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

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, Nick S. [email protected] 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)

<% objs.each do |o| %> <%= o.alts_for_select(o.current_id) %> <% end %>

The elsif (or case) structure is gone which is kind of nice.

ideas?

Thanks,

Peter

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 M.” [email protected] wrote
in message
news:[email protected]
On 3/15/06, Nick S.
[email protected] 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)

<% objs.each do |o| %> <%= o.alts_for_select(o.current_id) %> <% end %>

The elsif (or case) structure is gone which is kind of nice.

ideas?

Thanks,

Peter

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