Hi,

What is the correct syntax for adding a prompt to a select_tag?

I have this, which works as expected:
<%= select_tag "author_id", options_from_collection_for_select(@authors,
:id, :name, @selected_author) %>

When I try and add a prompt, like this:
<%= select_tag "author_id", options_from_collection_for_select(@authors,
:id, :name, @selected_author), :prompt => "Please select" %>

I just get:
<select id="author_id" name="author_id" prompt="Please select">
<option value="1">Jim</option>
...

However, adding a blank field works fine:
<%= select_tag "author_id", options_from_collection_for_select(@authors,
:id, :name, @selected_author), :include_blank => true %>

Resulting in:
<select id="author_id" name="author_id">
<option value=""></option>
<option value="1">Jim</option>
...

What am I missing?

Ruby 1.9.2p0
Rails 3.0.5

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Angelo Cordova at Jan 16, 2012 at 6:52 pm

    On 16 ene, 15:35, Jim Burgess wrote:
    Hi,

    What is the correct syntax for adding a prompt to a select_tag?

    I have this, which works as expected:
    <%= select_tag "author_id", options_from_collection_for_select(@authors,
    :id, :name, @selected_author) %>

    When I try and add a prompt, like this:
    <%= select_tag "author_id", options_from_collection_for_select(@authors,
    :id, :name, @selected_author), :prompt => "Please select" %>

    I just get:
    <select id="author_id" name="author_id" prompt="Please select">
    <option value="1">Jim</option>
    ...

    However, adding a blank field works fine:
    <%= select_tag "author_id", options_from_collection_for_select(@authors,
    :id, :name, @selected_author), :include_blank => true %>

    Resulting in:
    <select id="author_id" name="author_id">
    <option value=""></option>
    <option value="1">Jim</option>
    ...

    What am I missing?

    Ruby 1.9.2p0
    Rails 3.0.5

    --
    Posted viahttp://www.ruby-forum.com/.
    Have you tried with "options"?

    options = {:prompt => 'Please Select'}

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jim Burgess at Jan 16, 2012 at 7:00 pm

    Have you tried with "options"?
    options = {:prompt => 'Please Select'}
    Just did, but didn't work :-(
    The effect is unfortunately the same:

    <select id="author_id" name="author_id" prompt="Please Select">
    <option value="1">Jim</option>
    ...

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tim Shaffer at Jan 16, 2012 at 7:09 pm
    Try this:

    :include_blank => "Please select

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ysC9cdWs6J8J.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tim Shaffer at Jan 16, 2012 at 7:09 pm
    Try this. You can pass a string to include_blank

    :include_blank => "Please select"

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/r-jglcjPF-IJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jim Burgess at Jan 16, 2012 at 7:32 pm

    Tim Shaffer wrote in post #1041162:
    Try this. You can pass a string to include_blank
    :include_blank => "Please select"
    Thanks a lot, that works fine!

    Out of interest though, this passes "author_id" in the params as an
    empty string, so I have to write:
    if params.has_key?(:author_id) and not params[:author_id].empty?

    instead of just:
    if params.has_key?(:author_id)

    Does prompt pass any value in in the params?

    It would also be nice to understand why :prompt => true isn't working.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tim Shaffer at Jan 16, 2012 at 7:37 pm
    The :prompt option was not added until rails 3.1.0

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/cSLW0PgNtgcJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jim Burgess at Jan 16, 2012 at 7:40 pm

    Tim Shaffer wrote in post #1041166:
    The :prompt option was not added until rails 3.1.0
    Now I understand.
    I'll go with what you suggested.
    Thanks a lot for your help.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 16, '12 at 6:35p
activeJan 16, '12 at 7:40p
posts8
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase