Topic: Returning a Multiple select

Hello. I'm just having a moment when I can't think of what I need.

I have a multiple select which I select usually between 2 and 6 different values. How do I retrieve which values are selected?

The code I have below returns the highest id of the selected crew members. What I want is to get back either an array of crewmembers, or an array of the crewmembers' ids or something useable like that.

Thanks for any help - I'm still new at Ruby...

<p>
<b>Crew Members</b><br/>
Hold Control to Select multiple members<br/>
<SELECT NAME="crew" MULTIPLE SIZE=5>
<% @crewmembers.each do |crewmember| %>
<option value="crewmember.id">
<%= crewmember.lastname %>, <%= crewmember.firstname %>
</option>
<% end %>
</SELECT>
</p>

def create
    @job = Job.new(@params['job'])
    if @job.save
        redirect_to :action => 'list'
    else
        render_action 'new'
    end
    @job.add_crewmembers(@params['crew'])   
end

edit: Made for pretty printing in the code

Last edited by Caydel (2006-07-12 08:22:49)

Check out my Blog.

Re: Returning a Multiple select

@params[:crew] should be an array/hash (not sure which, haven't used a multiple select yet with Rails). You can then cycle through and see which values got sent.

Try

debug @params[:crew]

in your view to see exactly how it's POSTing the data (or alterntaively wait for someone who's actually handled multiple selects in Rails to tell you hehe).

Re: Returning a Multiple select

Here is the actual string it POSTs:

Parameters: {"crew"=>"crew[1]", "job"=>{"comments"=>"TEST TEST TEST", "location_id"=>"1"}}

Check out my Blog.

Re: Returning a Multiple select

You need to change your form a bit as rails doesn't handle multiple select lists the way it does other form values.

<select name="crew[]" multiple="multiple" size='5'>
<% @crewmembers.each do |crewmember| %>
<%= options_from_collection_for_select(@crewmembers, :id, :lastname + ", " + :firstname) %>
</select>

You'll want to be sure I wrote in the string concatination correctly, I'm new to ruby and rails.

Then you can handle the crew[] parameter in your controller with something like this:

for crewmember in params[:crew]
  #insert what you want to do with the value of the select option
  #(probably inserting it into a database
end

I had a lot of trouble understanding how to pass in the select option in the edit version of the form.  If you have that problem here are my posts:
1 or 2.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Returning a Multiple select

Thankyou, circuitshaman. I was just starting to think it was something like that, from some of the reading I was doing. It was good that you could confirm it to me. I really wasn't sure!

Thanks again!

Check out my Blog.

Re: Returning a Multiple select

Hmmm.... now I am getting this.

Dang... I can't wait until I learn more Ruby, and don't have to ask these trivial syntax questions...

--------------------------------------

NoMethodError in Job#new

Showing app/views/job/new.rhtml where line #20 raised:

undefined method `+' for :lastname:Symbol

Extracted source (around line #20):

17:  <b>Crew Members</b><br/>
18:  Hold Control to Select multiple members<br/>
19:  <select name="crew[]" multiple="multiple" size='5'>
20:  <%= options_from_collection_for_select(@crewmembers, :id, :lastname + ", " + :firstname) %>
21:  </select>
22:  </p>
23:

Check out my Blog.

Re: Returning a Multiple select

You are trying to add strings to symbols which doesn't work, try this:

<%= options_from_collection_for_select(@crewmembers, :id, :name) %>

I'm assuming a crewmember has no "name" attribute, so we can add one:

class CrewMember < ActiveRecord::Base
  def name
    "#{lastname}, #{firstname}"
  end
end

I haven't tested that, but hopefully it will work.

Railscasts - Free Ruby on Rails Screencasts

Re: Returning a Multiple select

That works, Ryanb.

Thanks for the pointers, everyone!

Check out my Blog.