简体   繁体   中英

formtastic select, one entry selected as default

I have an input which I render :as => :select. I would like one option to be selected as default. How would I go about that?

Thanks, Hendrik

If you are not making a form off of a model.

= semantic_form_for :report do |f|
  = f.inputs name: 'Choose a Report Year' do
    = f.input :report_year, collection: options_for_select(['2010', '2011', '2012', '2013'], '2012')
    = f.action :submit, label: 'Submit Year', as: :button

So set a preselected answer you have to associate your object with the desired value.

countries = ['USA','Germany']
@user.country = countries.first

That works for me.

You could read about this in the formtastic wiki. Always check gem readme before asking anything. Here is a link. Good luck!

If you don't have an associated model into the semantic_form_for you can do this:

f.inputs do
  f.input :service_type, :as => :radio,
          :collection => [
                           ['Group', 'Group', { :'checked' => 'checked' }],
                           ['Device', 'Device', {}]
                         ]
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM