How to pre select a state in Rails Carmen state_select - ruby-on-rails

What are the options I can pass in a state_select. I am trying to do :selected => state and its not working.
<%= f.state_select :state, 'US', {:prompt => '--Select One--', :selected => state}, :tabindex => autotab, :id => 'state_select, :state => state'%>

I don't think you can use state_select to do it. You'll have to use select with the state_options_for_select function in order to enter your selected state:
<%= f.select :state, state_options_for_select(state_to_select, country_code) %>
That should get you there.

Related

f.country_select options default country

In my rails app I want the f.country_select to be user's country, if the user has provided the country. Else I want it to have the Select one value.
<%= f.country_select :country, nil, {:selected => #user.country, :include_blank => 'Select one'}, {:class=>'form-control'} %>
Anything wrong with the above code?
Update:
Am using the gem 'country_select'.
It should be like this:
<%= f.select(:country, options_for_select(Country, :selected => #user.country), :prompt => 'Select one' , {:class=>'form-control'}) %>

rails input selected values from params to select_tag (multiple => true)

I want to keep the select_tag(:multiple => true) options to be selected which were selected by user once search is performed
<%= select_tag 'values[]', method_for_options_for_select, :class => 'some-class', :multiple => true, :size => 6 %>
Suppose a user select 4 values from the select tag then for values should be selected,
How can we pass this 4 values to the select_tag?
I tried using :selected => params['values[]'] but this doesnt works for multiple true
Any help will be appreciated
Ref this and options_for_select
Something like following
<%= select_tag 'values[]',
options_for_select(#stores.map {|s| [s.store_name, s.store_id]},
#user.stores.map {|j| j.store_id}),
:class => 'some-class', :multiple => true, :size => 6 %>

How to refresh the web page when user select a particular value on dropdown list on rails?

I'm developing project on rails 2.3.8 and I need to refresh the whole web page when user select particular selection on drop-down menu. How can I do it on rails ?
This is my drop down menu
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx') %>
<%= observe_field('xx', :url => { :controller => 'calendar', :action => 'update_country' },:update => 'app_header',:with => "'con=' + escape(value)")%>
This finely load the countries so how I can reload the whole page ? please can some one explain me about this?
Simply add a html option to your collection select , onchange => "Javascript code to reload the page"
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = '#{root_url}'") %>
Or you can just refresh the current page:
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = window.location.href") %>

Rails simple_form association help with option tag

In my form I have:
<%= f.association :virksomhed, :collection => Virksomhed.all(:order => 'navn'), :prompt => "Vælg virksomhed" %>
In view I have this:
I instead of the object I want to display the name (navn on danish) of the companyies.
I believe you're looking for the :label_method option:
<%= f.association :virksomhed, :collection => Virksomhed.all(:order => 'navn'), :prompt => "Vælg virksomhed", :label_method=>:navn %>

nil :selected value for date helper in formtastic

I'm using the following code for a date field:
<%= f.input :date_of_birth, :selected => nil,
:order => [:day, :month, :year],
:prompt => {:day => "Day", :month => "Month", :year => "Year"},
:start_year => Time.now.year - 15,
:end_year => Time.now.year - 100 %>
Everything works as expected, except for :selected => nil. I'm using 0.9.7, all the specs pass, including the one regarding a nil :selected value. When I use this though, the current date is selected, save for the year, which doesn't exist.
Am I missing something here? I tried with :as => :date and that did't make any difference.
Tom
This is a known regression/bug in Formtastic right now. Expect a fix in the next gem release.
You could try to add :include_blank => true, maybe that helps?

Resources