I have this piece of code, while using simple_form:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes
How can I preselect a specific preset checkbox, knowing that the ID of this preset is passed within params[:preset_id]? The checkboxes' HTML name attributes are report[preset_ids][].
According to the simple_form documentation:
The association helper just invokes
input under the hood, so all options
available to :select, :radio and
:check_boxes are also available to
association. Additionally, you can
specify the collection by hand, all
together with the prompt:
f.association :company, :collection
=> Company.active.all(:order => 'name'), :prompt => "Choose a Company"
So, you should use something like this:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes,
:checked => params[:preset_id]
I don't have experience with simple_form, but this might help :)
An update for everybody. the :selected option did not work for me. I used:
:checked => [2, 3]
Hope it helps someone.
f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (#claudio-acciaresi) that's why I'm commenting here...
This is my snippet:
<%= f.association :association, collection: Model.all,
value_method: :id, label_method: :name,
as: :check_boxes, include_blank: false %>
Replace symbol :association with the current has_many from the model.
Replace Model.all, for your source data.
Hope it gets useful for someone else :)
Regards.
Don't forget to cast params[:preset_id] to integer:
params[:preset_id].to_i
Related
I am using simple_form gem, I have a countries collection, it work fine when I select the country, and updated record will have the country id stored, but, when I try to edit the record, the chosen country is not selected by default at edit form.
Here is the code at edit form:
= f.input :country_id, :collection => all_countries
Shouldn't simple_form view the selected country from the db ?
Have you tried to use the :selected => option?
:selected => selected_country_id
So,
= f.input :country_id, :collection => all_countries, :selected => selected_country_id
This will work perfectly !!!
Cheers!
I know this has been answered, but I came here looking for a similar solution for a collection of check boxes. For posterity, here's how you do it:
<%= f.input :country_ids, :as => :check_boxes, :collection => [['USA', :USA], ['Japan', :JPN]], :checked => [:JPN], :include_hidden => false %>
Hope this helps someone.
How can I add two label elements when choosing association in Simple Form on Ruby on Rails?
Sample: #user.name = "Barack" and
#user.last_name = "Obama"
Here is my code:
<%= f.association :persona, :collection => Persona.order(:name),
:prompt => 'Choose a person' %>
It displays only Barack but I need it to display not only name but also last_name when choosing from list.
<%= f.association :persona, :collection => Persona.order(:name), :label_method => lambda { |persona| "#{persona.name} #{persona.last_name}" }, :prompt => 'Choose a person'%>
Here is the answer - you need a complex label_method.
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
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 %>
When I do this: <%= f.association :user, :collection => User.where(:country_id => 1) %>
My dropdown is populated with lines like this:
#<User:0x0000010b98d170>
Instead, I would like to display an email, that is tied to the id of the users.
I have yet to find how to override the value / content defaults of simple_form when using associations.
Can anyone help?
thank you,
P.
Although the page on github (https://github.com/plataformatec/simple_form) didn't say, but I guess it's the same as the example f.input :age, :collection => 18..60
You could use :label_method and :value_method:
f.association :user, :collection => User.where(:country_id => 1), :label_method => :name, :value_method => :id
I did not use it before. Please tell me if it does not work.