Options_for_select and default value

Hi, I often need yes/no drop down menus - DRY ? Okay, so
def select_yes_no(object, method, default = true)
return select(object, method, {‘Yes’ => true, ‘No’ => false},
:selected => default )
end

It works fine for a new record but the default value is always selected,
even when editing an old record

How to use select(), have a default choice selected when new_record? and
have the record value when not new_record?

Thank you

nuno wrote:

Hi, I often need yes/no drop down menus - DRY ? Okay, so
def select_yes_no(object, method, default = true)
return select(object, method, {‘Yes’ => true, ‘No’ => false},
:selected => default )
end

It works fine for a new record but the default value is always selected,
even when editing an old record

How to use select(), have a default choice selected when new_record? and
have the record value when not new_record?

Thank you

Where do you have this code - in a helper?

The select helper will set the starting value of the dropdown according
to the value of that field in an @object instance variable. It would
appear that where the select helper is getting called the @object object
doesn’t exist.

c.

Cayce B. wrote:

nuno wrote:

Hi, I often need yes/no drop down menus - DRY ? Okay, so
def select_yes_no(object, method, default = true)
return select(object, method, {‘Yes’ => true, ‘No’ => false},
:selected => default )
end

It works fine for a new record but the default value is always selected,
even when editing an old record

How to use select(), have a default choice selected when new_record? and
have the record value when not new_record?

Thank you

Where do you have this code - in a helper?

The select helper will set the starting value of the dropdown according
to the value of that field in an @object instance variable. It would
appear that where the select helper is getting called the @object object
doesn’t exist.

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

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.

Cayce B. wrote:

nuno wrote:

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?

The “edit” action of the “movements” controller generate two instances
with class instance scope : @move_dep and @move_arr

Then, the edit.rhtml view is rendered which renders the
_editdetails.rhtml partial twice because the @move_dep and @move_arr
must be treated at the same time (move_arr is an arrival and move_dep is
a departure), so I need to display them both on screen

While rendering the partial @move_dep and @move_arr ARE defined

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.

But how is the default value selected without any :selected parameter
(which you don’t use in your example) ?

There is something weird, I must be missing something or doing something
wrong

I forgot to say that suffix is defined to and passed as a local to the
partial

nuno wrote:

I forgot to say that suffix is defined to and passed as a local to the
partial

So, suffix can be “dep” or can be “arr”? In that case, I apologize - I’m
stumped - from here it looks like that should be working for you. The
only other thing I can think of is that you have to make sure the
current value of @move_dep.precision actually does match one of the
options that you give to the select() helper in the third variable.

For instance, if the value of @move_dep.precision is “YES”, but the
values of your options in the select() helper are “Y” and “N”, then it’s
never going to match and you’ll always get the default value.

But how is the default value selected without any :selected parameter
which you don’t use in your example) ?

That’s how Rails works. If there is an instance variable @move_dep with
a .precision method, then select(‘move_dep’, ‘precision’, blah-blah)
will create a tag with one or more tags (the
blah-blah), and the tag whose value matches the current value
of @move_dep.precision is going to get marked automatically with the
“selected” attribute.

c.

c.

Cayce B. wrote:

That’s how Rails works. If there is an instance variable @move_dep with
a .precision method, then select(‘move_dep’, ‘precision’, blah-blah)
will create a tag with one or more tags (the
blah-blah), and the tag whose value matches the current value
of @move_dep.precision is going to get marked automatically with the
“selected” attribute.

I don’t understand your point of view : for me, there is always an
instance because movement_XXX is equal to Movement.find or Movement.new
depending if we’re creating or modifying the record…

nuno wrote:

Cayce B. wrote:

That’s how Rails works. If there is an instance variable @move_dep with
a .precision method, then select(‘move_dep’, ‘precision’, blah-blah)
will create a tag with one or more tags (the
blah-blah), and the tag whose value matches the current value
of @move_dep.precision is going to get marked automatically with the
“selected” attribute.

I don’t understand your point of view : for me, there is always an
instance because movement_XXX is equal to Movement.find or Movement.new
depending if we’re creating or modifying the record…

Is it “move” or “movement” (you changed in this last post)?

I notice something interesting, reading back, that might be the issue…

<%= select(“move_#{suffix}”, ‘precision’, Move.precisions.invert %>

Since you are using “Move.precisions” here I would assume that you have
Move and Precision models, and you have them associated. Is the field in
your moves table maybe “precision_id” instead of “precision”? If that is
the case, then your select might should look like this instead:

<%= select(“move_#{suffix}”, ‘precision_id’, Move.precisions.invert %>

And if all this is the case, then that is also the reason you are not
defaulting correctly - Rails is trying to get the value of
Move.precision when that doesn’t really exist, it’s Move.precision_id,
so without a value it’s going with the default.

This is about as far as I can get without seeing your code. I’d be happy
to take a look if you want to post up the code from your controller
action method and the view itself where you are trying to use the select
helper and we can probably make more progress.

c.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs