I’m rendering my form in a partial, as below:

<%= f.collection_select :color_id, @colors, :id, :name, {:include_blank
=> false, :prompt => ‘What color?’} %>

Now, I’m rendering this partial in both the new and edit methods of my
controller. Everything works great, except for one caveat.

If the user deletes the color that was assigned prior, this results in
the parent object being assigned a color ID that doesn’t exist.

It doesn’t throw up any errors, but when you go into the edit method of
the parent controller to change the color, another color is
automatically selected. It seems that the options for :prompt are
ignored when the value is passed when in the edit method for example,
even if that value no longer exists.

What I’d like for it to do is to show the prompt - even in the edit
method - if the value doesn’t exist. Any way to do this?

Thanks in advance for any help at all.

Fixed it. Turns out I was using collection_select wrong. This works

<%= f.collection_select :items, :color_id, @colors, :id, :name,
=> false, :prompt => ‘What color?’} %>

Looks like this is already known:

Actually - nevermind. I thought it fixed the issue but turns out it’s
still not working right. I was still using the collection_select wrong,
I believe - so at least that was fixed.

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