Rails - Simple Form - Force a non-multiple select input field - ruby-on-rails

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 } %>

Related

Hover over display related text for simple form association field

I am using Rails and simple form. I have a collection that I am displaying with an association field. For each item in the collection, I want to display a hover over title of an attribute belonging to that item, namely description.
Here is my association:
<%= f.association :user_roles, collection: #roles, as: :check_boxes, label_method: :name, label: false %>
I want to display a hover over text of the description of each role. In my head something like the following should be doable:
<%= f.association :user_roles, collection: #roles, wrapper_html: {title: UserRole.where(id: this_current_items_id).first.description}, as: :check_boxes, label_method: :name, label: false %>
or:
<%= f.association :user_roles, collection: #roles, wrapper_html: {title: :description}, as: :check_boxes, label_method: :name, label: false %>
Neither of these work obviously but in my head a solution like this should be easy. I guess the guys at simple form never thought of this situation.
However, is this possible to do? Or, how can I get the ID of the current item so that I am displaying that items description on hover over?
PS: What I need is a simple form hover_method!
You can easily add other attributes to each item in your collection like so:
<%= f.association :user_roles, collection: #roles.map { |r| [r.name, r.id, { title: r.description, "data-toggle": "hover" }] }, as: :check_boxes, label: false %>

Two Association Label Elements in Simple Form

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.

Ruby on Rails 4 - simple_form multiple select input

I have a simple_form input field that looks like this:
<%= f.input :particular_users, collection: #all_users, input_html: { class: 'multiselectuser', multiple: true} %>
When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.
What am I doing wrong?
EDIT: I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.
f.input :days, collection: #your_collection, input_html: { multiple: true }
It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).
To create multiple select tags with simple_form, use:
<%= f.association :particular_users, collection: #all_users, input_html: { class: 'multiselectuser'} %>
see part Associations in the gem description.
But as you don't want to use an ActiveRecord associaion, use select_tag:
<%= select_tag 'particular_users',
options_from_collection_for_select(#all_users, :id, :name),
multiple: true, class: 'multiselectuser' %>

Uppercase collection with simple_form

How would one capitalise the first letter of a list of country's in below code example?
My question is regarding simple_form, not a regular form for which one could use collection_select.
= p.input :state,
:readonly => "readonly",
:collection => DataState.where(:country => "1"),
:selected => 1,
:id => "state",
:name => "state",
:prompt => "Please choose"
The problem is the list of states is in the database and saved like "alamaba","new york"
I need it to be down cased in the database but would like to uppercase/capitalize the first letter for visual clarity.
Try this:
In your DataState model, add a method that displays a capitalized version of your state names
class DataState < ActiveRecord::Base
def capitalized_state
state.capitalize
end
end
then use this new method to list your choices
Depending on if you're trying to save the id of the state as an association or just the string, the erb would be either
<%= f.input :state, collection: DataState.where(:country => "1").pluck(:capitalized_state) %>
or
<%= f.input :state, collection: DataState.where(:country => "1"), value_method: :id, label_method: :capitalized_state %>
where the former is for the state name and the latter is for the association id.
see this: using capitalize on a collection_select
Edit: I noticed that your html does not specify what attribute of the DataState class you're using as the text for your choices. If you're using rails, you should look into the collection_select form helper.
One way of going about it, too, would be the addition of the text-transform: uppercase property of css, looking like this:
<% = f.input: state, collection: DataState.where (: country => "1"), style: 'text-transform: uppercase;' %>

How to use a table column in my select box on rails?

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" } %>

Resources