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.
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matthew collins (Guest)
on 2006-02-19 18: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?
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-02-19 18:23
(Received via mailing list)
Matthew,

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

--
DeLynn Berry
delynn@gmail.com
http://www.delynnberry.com
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matthew collins (Guest)
on 2006-02-19 19: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?
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-02-19 20: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 Berry
delynn@gmail.com
http://www.delynnberry.com
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matthew collins (Guest)
on 2006-02-19 20: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
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-02-19 21: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 Berry
delynn@gmail.com
http://www.delynnberry.com
2899cec9af525f7953e19c8210aca97c?d=identicon&s=25 Jason Perkins (Guest)
on 2006-02-19 21: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 Perkins
jperkins@sneer.org

"The computer allows you to make mistakes
faster than any other invention, with the
possible exception of handguns and tequila."
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matthew collins (Guest)
on 2006-02-19 22: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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-19 23: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.