ruby on rails - How can set a default value in select tag? -
i have table , want set default value
here table:
|customers| |id| |name| 1 abc 2 def 3 ghi 4 jkl
here controller:
@customers.customer.all @customer_selected = customer.find(:all,:conditions=>['id=1'])
here view, showing customers
<%= select_tag "customer_id", options_for_select(@customers.collect {|t| [t.name,t.id]},params[:customer_id].to_i) %>
i tried line select customer_id=1 default value (selected)
<%= select_tag "customer_id", options_for_select(@customers.collect {|t| [t.name,t.id]},params[:customer_id].to_i),:selected =>@customer_selected.name %>
i'm getting error
undefined method `name' #<array:0x7f31d8098560>
please can me ?
one thing bear in mind if params[:customer_id] isn't there, equal nil, , nil.to_i
gives 0. should fine shouldn't have customer id 0 it's aware of.
i think should work:
<% default_id = params[:customer_id] ? params[:customer_id].to_i : (@customer_selected && @customer_selected.id) %> <%= select_tag "customer_id", options_for_select(@customers.collect {|t| [t.name,t.id]}, default_id) %>
this use params[:customer_id]
if it's defined, or @customer_selected.id
if @customer_selected
defined.
Comments
Post a Comment