Select prompt option disappears when validation fails in Rails
Let me preface by saying I'm a noob to Rails and StackOverflow so please go easy on me. I'm using Rails 2.3.8 with sqlite3 on my dev box.
I have created a select pulldown in a f开发者_开发技巧orm using the following:
<%= select( "communication", "gig_id", { "Add New Gig" => "new"}, {:prompt => "-- Select Gig --"}, :onchange => "toggle(this, 'gigInfo')") %>
However, when something else in the form fails validation and the "new" page is re-rendered, my prompt goes away and the only option left is the "Add New Gig" option. This is the case with ALL my forms and I can't seem to find any answer as to why.
My controller uses the basic scaffolding so I'm sort of at a loss. Any help would be greatly appreciated.
Your observation is accurate, and you astutely observed that :include_blank
remains even when :prompt
does not.
You can achieve the results you want by setting :include_blank
to the string you were using for prompt (it doesn't need to be a boolean).
:prompt
, it seems, only appears when there is no value for Rails to supply the given field. When your app re-renders the new
view, there is a value to supply that field because it created an instance of your Communication model. (It failed to save that instance, but it looks as though that instance has its gig_id
field set.)
精彩评论