Rails simple_form label for select - ruby-on-rails

I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>

This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.

Related

Translating form_for syntax to simple_form syntax

I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).

Using simple_form in Rails 4, trying to pass data attributes to an input_field

I'm using simple_form in Rails 4, and I was using an input that I then passed data attributes to like so:
<%= f.input :url, :input_html => {"data-toggle" => "tooltip", :title => "My tooltip"} %>
and it worked as expected, creating tags like:
<input data-toggle="tooltip" data-original-title="My tooltip">
Now, however, I need to use simple_form's input_field so I can have more control over the display, but it doesn't seem to accept the input_html argument. My code looks like:
<%= f.input_field :url, :input_html => {"data-toggle" => "tooltip", :title => "My tooltip"} %>
and it results in:
<input html="{:data=>{"data-toggle"=>"tooltip", :title=>"My tooltip"}}">
Which is clearly suboptimal (I stripped other irrelevant properties and attributes to simplify). Any ideas on how to make this work?
Looking at the code for SimpleForm::FormBuilder#input_field, it appears that all options passed in are treated as if they were under :input_html.
Try removing :input_html and just passing the options directly:
<%= f.input_field :url, "data-toggle" => "tooltip", :title => "My tooltip" %>

Show a hidden field with simple_form

I have a hidden field as such:
<%= f.input :authentication, label: 'Authentication Password', as: :hidden %>
I want to show the hidden field when the admin radio button is clicked:
<%= f.input :account_type , as: :radio_buttons, :checked => 'Student', collection: ['Student', 'Admin'], wrapper: :vertical_radio_and_checkboxes %>
How would I do so?
I have tried using
$('#user_authentication').show();
But this does not work.
Remove as: hidden and use
$('#user_authentication').hide(); when DOM is loaded.

Use mb_chars on simple_form RUBY

I'm using the gem simple_form and trying to do a ".mb_chars" on my field. Any idea?
My field:
<%= f.input :metadesc_mb, label: 'Meta description' %>
Maybe something like this (don't work):
<%= f.input :metadesc_mb.mb_chars, label: 'Meta description' %>
Thanks in advance.
I believe you should use :value_method key to pass the string processed with #mb_chars. That will
something like the following:
<%= f.input :metadesc_mb, label: 'Meta description', value_method: -> { self.metadesc_mb.mb_chars } -%>
or to use :value subkey of :input_html key:
<%= f.input :metadesc_mb, label: 'Meta description', input_html: { value: self.metadesc_mb.mb_chars } -%>
NOTE: The last method can't be applied to collections.

How to make the f.select rails selected

My code:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']]), {}, {:class => 'span3 controls controls-row'}, :selected => params[:area] %>
The result is:
ArgumentError in Users#edit
Showing /home/airson/rails_projects/friends_of_local/app/views/users/edit.html.erb where line #17 raised:
wrong number of arguments (5 for 4)
Why am I getting this error?
No need to use :selected just pass your params[:area] alone to options_for_select as a second argument:
<%= f.select :area,
options_for_select([['a','a'],['b','b'],['c','c']], params[:area]),
{}, { :class => 'span3 controls controls-row' } %>
The last value of your params[:area] will be selected.
Hope it helps ; )
You should pass :selected option to options_for_select method, like this:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']], :selected => params[:area]), {}, { :class => 'span3 controls controls-row' } %>
The above answers didn't work for me on Rails 6.
This is what worked. Copied from one of the answers on Reddit
= f.select(:results, options_for_select(['Accepted', 'Not Accepted', 'Rejected', 'Acc', 'Rej', 'Information Only', 'N/A'], selected: #report.results || nil), { include_blank: "Select Result" }, { class: 'form-control select2', style: 'width: 100%;'})
Below is what worked for me using f.select with custom names and values including a blank field while using the Ransack search. Thanks to mayorsanmayor's example for getting me started in the right direction. This also works for rails 5.1.7.
<%= f.select(:status_eq, options_for_select([ ["Verified", "verified"], ["Non Verified","notVerified"]], selected: #q.status_eq || nil), {:include_blank => "Select Status"}, {class: 'form-control'}) %>

Resources