Topic: selects problem

I hate select boxes in Rails

How do I get the select box to have the default box set from the params[:id]?

 @projects = Project.find(:all, :conditions => ["tipe = 'budget' and client_id = ?", @user.client_id], :order => "created_on")
form.collection_select(:project_id, @projects, :id, :name)

I may have this all backwards but I was under the impressioin if I had @project.id set to the db row that I wanted the select box to default it would. but no. so I am either under the wrong impression or I'm doing soemthing wrong.

thanks

--

Re: selects problem

Looks like you're using form_for, can you post the rest of the code so I can see what model form_for is using? Rails uses this model to set the default value, so you would set the project_id attribute in this model to whatever you want the default to be.

Railscasts - Free Ruby on Rails Screencasts

Re: selects problem

yes it is form_for

the model, I was setting it in the controller. I'll give it a shot when I get home

class Project < ActiveRecord::Base
  belongs_to :client
  has_many :actions
 
  validates_presence_of :name, :tipe, :client_id
end

--

Re: selects problem

You could try setting the default value in the model itself, but instead I recommend doing it in the "new" action. First off, what model are you editing in this form? I'm assuming it is "action" because it has_many projects. You would do something like this in the "new" action:

# in controller
def new
  @action = Action.new(:project_id => 1)
end

This would default the action to the project with the id "1". It's not good to hard-code ids in here, but I don't know enough about the app to make a better suggestion - you'll have to make the id dynamic somehow.

Railscasts - Free Ruby on Rails Screencasts