Topic: Still struggling with selection option in form helpers

I have a simple selection list in a form.  I'd like it to select the option with the same idea as a variable passed by a link tag.  I've tried 3 different ways of creating the selection list now and still can't seem to get it working.

The link:

<%= link_to 'Add Task', :controller => "tasks", :action => 'new', :project_id => project %>

The recieving partial:

<label for="task_project_id">Associated Project</label><br/>
<select id="task_project_id" name="task[project_id]" >
<%=
  @projects = Project.find(:all)
  options_for_select(@projects.collect {|p| [p.name,p.id] }, params[:project_id])
%>

</select>


P.S. How do you make the code in these forms have line numbers and pretty formatting.  I figure if you magnificent moderaters are willing to help me through the learning curve I might as well make things a little more readable.

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

Had an epiphany.  I stuck it in the controller instead of trying to handle it in the view.  I'd still love to get an explination of how the form helpes handle auto filling the values in the form when the instance variable is passed to them from the controller.  Here is the code that works:

  def new
    @task = Task.new
    @task.project_id = params[:project_id] if params[:project_id]
  end

<%= 
  @projects = Project.find(:all)
  collection_select("task", "project_id", @projects, :id, :name)
%>

Though I still would like an answer to my PS about the pretty formatting of code smile

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

See this post for making the code pretty.

As for your problem, it may help to know there are two different types of form helpers. There are model specific helpers which are design to relate directly to a model's attribute. Then there are general form helpers which usually end with "_tag" and don't relate to models in any way.

Every model-specific helper has you specify the name of the model and the attribute you want it to alter. That helper will then look for an instance variable with the same name as the model, so in your case, it looks for the instance variable @task and uses the value of the project_id attribute to set the default value of the form element.

Railscasts - Free Ruby on Rails Screencasts

Re: Still struggling with selection option in form helpers

Thank you, that helps me understand all the CRUD much better.

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

Your code's been prettyfied big_smile

vinnie - rails forum admin

Re: Still struggling with selection option in form helpers

I thought I completely understood this problem, and I'm grateful to you peeps for that, but I've got a strange thing happening.

I can't seem to get a select box I have to accept the selected values to highlight the darn item!  I blamed rails form helpers for so long that I decided to write out the option tags in a block myself.  But it still didn't highlight them.

I need some fresh eyes to tell me where I've gone wrong.

def work_by_persons
  @users = User.find(params[:userlist]) if params[:userlist]
  @report = "Work By Person(s)"
  @user_id_list = params[:userlist]
  @sql_time_filter = sqlify_time_filter(params[:time_filter])
end

<h1 class='heading'>Work By Person(s)</h1>

<%= form_tag :action => "work_by_persons" %>
<div class='form-item'>
<label for='user-list'>User Listing</label><br />
<select name="userlist[]" multiple='multiple' size='5'>
<%=
  @users = User.find(:all, :order => "name")
  options_for_select @users.collect { |u| [u.name, u.id] }, @user_id_list
%>
</select>
<div class='description'>Select a number of users to report on above and click generate</div>
</div>
<div class='form-item'>
<input type='hidden' name='time_filter' value='<%= params[:time_filter] %>' />
<%= submit_tag "Generate Report" %>
</div>
<%= end_form_tag %>

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

What is the value of the @user_id_list variable? Is it a number? A string? if it is a number as a string it might not be matching the user id which is an integer. Try:

options_for_select @users.collect { |u| [u.name, u.id] }, @user_id_list.to_i

If you want to support multiple selection, then it should be an array of integers (ids) that you want selected.

Railscasts - Free Ruby on Rails Screencasts

Re: Still struggling with selection option in form helpers

It's a list of id's and they are read in as strings, and I tried type casting them to see if that was the problem before, but it didn't work.  However I've tried again, trusting your expert knowledge with the following code:

options_for_select @users.collect { |u| [u.name, u.id] }, @user_id_list.each {|user_id| user_id.to_i }

But it still didn't work.

Last edited by circuitshaman (2006-07-18 16:54:30)

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

Try "collect" instead of "each".

options_for_select @users.collect { |u| [u.name, u.id] }, @user_id_list.collect {|user_id| user_id.to_i }

Railscasts - Free Ruby on Rails Screencasts

Re: Still struggling with selection option in form helpers

YES!  Thank you.  Now will you please explain to me the difference between each and collect?

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

-SICP (Abelson, Sussman)

Re: Still struggling with selection option in form helpers

each just iterates the array and returns the original array. In your case the string was being converted to a number, but it wasn't going anywhere after that since "to_i" method just returns a number and the "each" method ignores that.

collect iterates the array and returns a new array made up of the result of the block. Here's a few examples:


nums = [1, 2, 3]
nums.each { |n| puts(n+1) }
# returns [1, 2, 3] but prints 2 3 4

nums.collect { |n| n+1 }
# returns [2, 3, 4] but doesn't change the "nums" array

nums.collect! { |n| n+1 }
# returns [2, 3, 4] and changes the "nums" array to [2, 3, 4] (notice the exclamation mark)

nums.collect { |n| 5 }
# returns [5, 5, 5]


Hope that clears things up. I'd suggest playing around with it on the irb shell to get a better idea on how it works.

Railscasts - Free Ruby on Rails Screencasts

Re: Still struggling with selection option in form helpers

Thank you for taking the time to include extra examples as well.  It now makes perfect sense.  The more I know how ruby works the more I love it.  "I THINK I LOVE YOU..." sorry...  I'm satisfied with my move from python and zope.

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

-SICP (Abelson, Sussman)