Default value in select (ActionView)

Any suggestions on how to put a default value into a select method

eg. f.select :type_id, List.find(:all).collect {|p| [ p.name, p.id ]}

but if the :type_id is nil I want to be able to use the default value
from the List Model.

The :select option seems to override the method

nigel.bris wrote:

Any suggestions on how to put a default value into a select method

eg. f.select :type_id, List.find(:all).collect {|p| [ p.name, p.id ]}

but if the :type_id is nil I want to be able to use the default value
from the List Model.

Three ways:

  1. Put the default as the default value in the database field.
    It will automatically be that value rather than nil.

  2. Put in your controller:

    @object.type_id ||= List::DEFAULT_TYPE_ID

  3. Put in your List.initialize method:

    self.type_id = DEFAULT_TYPE_ID


We develop, watch us RoR, in numbers too big to ignore.

Great Ideas…

I probably should of given a bit more info…
List Model is used by multiple models and is a common place for list
items (grouped by the model ListGroup)
List model has a field called default (boolean) so the user can change
the default option.
The selection list lives in a helper because it is common to lots of
views.

I could do it in the controller, but this would require code each
time, as this is in a helper, I want it to write it once. Any other
suggestions?

nigel.bris wrote:

I could do it in the controller, but this would require code each
time, as this is in a helper, I want it to write it once. Any other
suggestions?

I’m not understanding how the boolean default field is used, but
another way would be to override the AR accessor method of type_id:

class List
def type_id
self[:type_id] || default_type
end
end


We develop, watch us RoR, in numbers too big to ignore.

HI Mark,

Thanks for you help here…

The default field is in the List model. The List model is open to the
client so they can add different options eg. States so if a client
than adds a customer card which has their address, the selection list
showing the states should defualt to the state marked as defualt in
List. I have created a helper to “my_selection” which is sent the List
group (eg states group) which means the list will only have states. To
keep it dry, I want the “my_selection” to see the item is nil and
select the default item.

Hi Mark,
Thought you might cast some light on these:

  1. How can I set a default value in a collection_select? To be specific,
    I have a class ‘Platform’, and I want to set the platform_code (a field
    of Platform) to be ‘ORA’ (which exists in the Platform table).
    I tried many variations of the following code in my view, but in vain:
    <% @selected = ‘ORA’ %>
    <%= collection_select “sac_table_platform”, “platform_code”,
    Platform.find(:all, :order => “platform_code”),
    :platform_code, :platform_code, { :selected => @selected } %>

  2. How can I build my own list of collection_select, e.g., a list of
    three people, say, to show [Adam, Bob, Charley] on the collection_select
    for the corresponding values [‘A’, ‘B’, ‘C’] in a table (which already
    exists), and use it in my view?

Thanks,
Arif

Mark Reginald J. wrote:

nigel.bris wrote:

I could do it in the controller, but this would require code each
time, as this is in a helper, I want it to write it once. Any other
suggestions?

I’m not understanding how the boolean default field is used, but
another way would be to override the AR accessor method of type_id:

class List
def type_id
self[:type_id] || default_type
end
end


We develop, watch us RoR, in numbers too big to ignore.

Niaz Arifin wrote:

:platform_code, :platform_code, { :selected => @selected } %>
Arif, just set @sac_table_platform.platform_code = ‘ORA’, either in
your controller or in Platform.initialize

  1. How can I build my own list of collection_select, e.g., a list of
    three people, say, to show [Adam, Bob, Charley] on the collection_select
    for the corresponding values [‘A’, ‘B’, ‘C’] in a table (which already
    exists), and use it in my view?

Just use an ordinary select having options parameter
[[‘Adam’,‘A’], [‘Bob’,‘B’], [‘Charley’,‘C’]]


We develop, watch us RoR, in numbers too big to ignore.

Hi Mark,
Thanks a lot… setting @sac_table_platform.platform_code = ‘ORA’ worked
for the default case, but it created a new problem for me: now when I
tried to specifically select a value (eg, ‘NSK’) from the
collection_select, it didn’t. Instead, it always selected ‘ORA’. I tried
to include a blank in the list, and put an if-condition in the
controller, but now it gets a null for ‘platform_code’.
Could you please tell me what I am doing wrong?
The relevant snippets are here:
In the controller:
def new
@create_date = @last_modify_date = getdate()
end
def create

params[:sac_table_platform][:table_id] =
params[:sac_table_config][:table_id]
params[:sac_table_platform][:table_config_id] =
params[:sac_table_config][:table_config_id]
@sac_table_platform =
SacTablePlatform.new(params[:sac_table_platform])
if params[:sac_table_platform][:platform_code].nil?
@sac_table_platform.platform_code = ‘ORA’
end

if @sac_table_config.save and @sac_table_platform.save
flash[:message] = { :title => ‘Information’, :msg => ‘SAC Table
Config and SAC Table Platform are successfully created.’ }
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

In the ‘new.rhtml’ view:
<%= collection_select (“sac_table_platform”, “platform_code”,
Platform.find(:all, :order => “platform_code”),
:platform_code, :platform_code, :include_blank => true ) %>


Error I got:
OCIError: ORA-01400: cannot insert NULL into
(“SAC_CFG”.“SAC_TABLE_PLATFORM”.“PLATFORM_CODE”): INSERT INTO
SAC_Table_Platform (table_name, table_id, table_config_id,
platform_code) VALUES (’’, 6140, 1, ‘’)

Parameters: {“name”=>“add_form”, “commit”=>“Create”,
“sac_table_platform”=>{“platform_code”=>"", “table_id”=>“6140”,
“table_name”=>"", “table_config_id”=>[#<SacTableConfig:0x4b16260
@attributes={“nextval”=>59.0}>]},
“sac_table_config”=>{“last_modify_date”=>#<Date: 4908771/2, 0, 2299161>,
“table_id”=>“6140”, “create_date”=>#<Date: 4908771/2, 0, 2299161>,
“change_description”=>“aswq”,
“table_config_id”=>[#<SacTableConfig:0x4b16260
@attributes={“nextval”=>59.0}>]}}

Thanks a lot again.

Mark Reginald J. wrote:

Niaz Arifin wrote:

:platform_code, :platform_code, { :selected => @selected } %>
Arif, just set @sac_table_platform.platform_code = ‘ORA’, either in
your controller or in Platform.initialize

  1. How can I build my own list of collection_select, e.g., a list of
    three people, say, to show [Adam, Bob, Charley] on the collection_select
    for the corresponding values [‘A’, ‘B’, ‘C’] in a table (which already
    exists), and use it in my view?

Just use an ordinary select having options parameter
[[‘Adam’,‘A’], [‘Bob’,‘B’], [‘Charley’,‘C’]]


We develop, watch us RoR, in numbers too big to ignore.

Niaz Arifin wrote:

Hi Mark,
Thanks a lot… setting @sac_table_platform.platform_code = ‘ORA’ worked
for the default case, but it created a new problem for me: now when I
tried to specifically select a value (eg, ‘NSK’) from the
collection_select, it didn’t. Instead, it always selected ‘ORA’. I tried
to include a blank in the list, and put an if-condition in the
controller, but now it gets a null for ‘platform_code’.
Could you please tell me what I am doing wrong?

You only want to set @sac_table_platform.platform_code = ‘ORA’
for a request.get?. So either (1), add this condition to an assignment
in the controller, (2), set platform_code in SacTablePlatform.initialize
using self.platform_code ||= ‘ORA’ after calling super, or (3), add
‘ORA’ as the default value for platform_code in your database table.


We develop, watch us RoR, in numbers too big to ignore.