Wednesday 21 March 2007

Selection List syntax

I dont think the explanation of 'selection lists' in Agile Web Development with Rails(1st ed) is very clear. It mentions:

select(:variable, :attribute, choices, options, html_options)

Where you can then set the default selection with:

@variable.attribute

However this variable is not available automatically, you need to create it yourself. The def in the api is more intuitively described as:

select(:my_object, :method, choices, options, html_options)

You must then create your own class with the accessor methods you need so that in the controller you can do something like:

def action
@my_object = MyClass.new
@my_object.method = params[:my_object][:method]
end

No comments: