Forum: Ruby on Rails select, first option empty value?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
matthew collins (Guest)
on 2006-02-19 19:11
(Received via mailing list)
using the below:

<%= select('project', 'project_type_id', @project_types.collect  {|x|
[x.name,x.id]}, {:include_blank => true}) %>

but i'd like that first option to have something like "select project
type" with an empty value?
DeLynn B. (Guest)
on 2006-02-19 19:23
(Received via mailing list)
Matthew,

What you are looking for is the :prompt symbol:
http://api.rubyonrails.com/classes/ActionView/Help....

--
DeLynn B.
removed_email_address@domain.invalid
http://www.delynnberry.com
matthew collins (Guest)
on 2006-02-19 20:24
(Received via mailing list)
my apologies, i guess i'm just not getting the syntax down.

i'm trying

<%= select('project', 'project_type_id', @project_types.collect  {|x|
[x.name,x.id]}, {:prompt => "pick something"}) %>

and it's not showing up?
DeLynn B. (Guest)
on 2006-02-19 21:22
(Received via mailing list)
> and it's not showing up?

Hmm, that's strange. It looks fine to me. Here's a snippet from my
application that is working:
<%= select("user", "id", @users.collect {|u| [u.name, u.id]}, {:prompt
=> "--Select One--"}) %>

--
DeLynn B.
removed_email_address@domain.invalid
http://www.delynnberry.com
matthew collins (Guest)
on 2006-02-19 21:55
(Received via mailing list)
strange indeed.

<p><label for="project_type">Project Type</label><br />
<%= select('project', 'project_type_id', @project_types.collect  {|x|
[x.name,x.id]}, {:prompt => "--Select One--"}) %></p>

and on the controller side, i have

@project_types = ProjectType.find_all()
@project = Project.new



ruby -v = 1.8.2
rails -v = 1.0.0
DeLynn B. (Guest)
on 2006-02-19 22:10
(Received via mailing list)
Hmmm, very interesting. I copied your code snippet directly into my
app and just changed the @project_types to @users and it worked just
fine. Could I see the generated HTML by chance?

Is there an item that is being selected? Is it just the first item in
the list or is it something else? Does your Project object have a
default project type perhaps? What happens if you comment out the
"@project = Project.new" bit in your contoller?

--
DeLynn B.
removed_email_address@domain.invalid
http://www.delynnberry.com
Jason P. (Guest)
on 2006-02-19 22:25
(Received via mailing list)
On Feb 19, 2006, at 1:53 PM, matthew collins wrote:

> strange indeed.
>
> <p><label for="project_type">Project Type</label><br />
> <%= select('project', 'project_type_id', @project_types.collect  {|
> x| [x.name,x.id]}, {:prompt => "--Select One--"}) %></p>
>
> and on the controller side, i have
>
> @project_types = ProjectType.find_all()
> @project = Project.new

Can you run script/console and then do:

@project_types = ProjectType.find_all()

to see what's returned? Alternatively, you can do:

debug(@project_types) in the view to see what it's set to. But the
console is going to let you explore further.



--
Jason P.
removed_email_address@domain.invalid

"The computer allows you to make mistakes
faster than any other invention, with the
possible exception of handguns and tequila."
matthew collins (Guest)
on 2006-02-19 23:20
(Received via mailing list)
script/console:
 @project_types = ProjectType.find_all()
=> [#<ProjectType:0xb7798a04 @attributes={"name"=>"read", "id"=>"3"}>,
#<ProjectType:0xb77989c8 @attributes={"name"=>"look", "id"=>"4"}>,
#<ProjectType:0xb779898c @attributes={"name"=>"listen", "id"=>"5"}>]


debug(@project_types)

---
- !ruby/object:ProjectType
  attributes:
    name: read
    id: "3"
- !ruby/object:ProjectType
  attributes:
    name: look
    id: "4"
- !ruby/object:ProjectType
  attributes:
    name: listen
    id: "5"

and, from another request, the generated html
<!--[form:project]-->
<p><label for="project_type">Project Type</label><br />
<select id="project_project_type_id"
name="project[project_type_id]"><option value="3">read</option>
<option value="4">look</option>
<option value="5">listen</option></select></p>

and i'm running this in the test environment on the webrick server.
Mark Reginald J. (Guest)
on 2006-02-20 00:20
(Received via mailing list)
matthew collins wrote:
> my apologies, i guess i'm just not getting the syntax down.
>
> i'm trying
>
> <%= select('project', 'project_type_id', @project_types.collect  {|x|
> [x.name,x.id]}, {:prompt => "pick something"}) %>
>
> and it's not showing up?

I think :prompt only works if project_type_id is defined "default null"
(and not "not null") in the database projects table.

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.