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.
Related
I am creating an input form for a Post model with Simple Form on a Rails app. The Post model is associated to a Keyword model with a has_and_belongs_to_many. To fill up the Tags in the form, I am using:
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword..." %>
which creates a html :select tag for the input. The problem is, because is a many to many association, Simple Form assigns the :multiple tag to :select by default, allowing selecting many objects. But I do want to force it to output a simple <select> with no multiple for this field.
Any idea how to do this? Thanks a lot!
Figured it out, pass :input_html => { :multiple => false }
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword...", :input_html => { :multiple => false } %>
I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
I have a two select fields and their default options are blank so I would like to set all of them to 'Choose one'. I can do it for individual fields but I want to do it in the config somehow (avoiding the redundancy).
form do |f|
f.inputs "Item" do
f.input :field_1, :prompt => 'Choose one', :foo
f.input :field_2, :prompt => 'Choose one', :bar
end
end
How do I do this? :)
Edit: These prompts would be used on many forms. It really needs to be a config thing.
Try using a array:
fields = [:field_1, :field2]
form do |f|
f.inputs "Item" do
fields.each { |field| f.input field, :prompt => 'Choose one' }
end
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 %>
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