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?
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?
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
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 } %>
Arif, just set @sac_table_platform.platform_code = 'ORA', either in
your controller or in Platform.initialize
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?
Just use an ordinary select having options parameter
[['Adam','A'], ['Bob','B'], ['Charley','C']]
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.