nuno wrote:
object and method are passed as String parameters, as in the api
select() method so it should work the same way…
In fact the problem is the same when I try to use :selected from a view.
eg
<%= select(“move_#{suffix}”, ‘precision’, Move.precisions.invert,
{:selected => :precise }) %>
The default is always selected, no matter if move_#{suffix} is a new
record or not…
So, I’m still finding for a solution to have :selected enabled ONLY for
new record without polluting the view with many ‘if’ statements
Thanks
But do you have an @move_#{suffix} instance variable defined?
A simpler example…
controller
def edit
@widget = Widget.find(1)
end
edit view
<%= select(‘widget’,‘flag’,[[‘Yes’,‘Y’],[‘No’,‘N’]]) %>
Since your select() helper object parm is ‘widget’, Rails looks for an
instance variable @widget, and maps the value in @widget.flag to the
selected value of the dropdown. If I take out the @widget= line in my
controller, then the selected value will always be the default.
So, for example, if in one case {suffix} is equal to ‘3’, then:
<%= select(“move_#{suffix}”, ‘precision’, Move.precisions.invert %>
is going to be looking for an instance variable @move_3 which is a Move
object. If it exists, then the value in @move_3.precision will be
selected; if it doesn’t then the default is selected.
c.