Placeholder in rails simpleform association - ruby-on-rails

How to put placeholder in rails simpleform association. I have tried a different ways but nothing works for me. The target code is:
<%= f.association :sex, :include_blank => true %>

If you are using Select2, you can include placeholders in select elements. The requirement is to include a blank element as first item and you can include the placeholder text as a data HTML attribute.
In your case:
<%= f.association :sex, include_blank: true,
data: { placeholder: 'Search...'} %>

Please check the simple_form.rb in initializers
You can find something below
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
.
.
.
config.wrappers :default, :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
## Extensions enabled by default
# Calculates placeholders automatically from I18n
# You can also pass a string as f.input :placeholder => "Placeholder"
b.use :placeholder
.
.
end
end
so it is depends on translational file
So you need to do for user model sex attribute
en:
simple_form:
placeholders:
user:
sex:
As placeholder works for input
Then you try this
<% f.association :user do |u| %>
<%= u.input :sex %>
<% end %>
And It should work

Using Rails 5.0.0.beta2 and Simple Form 3.2.1, this works for me:
<%= f.association :strands,
collection: Strand.order(label: :asc),
include_blank: "-- Select strands --"
%>
I had no luck using various permutations on :prompt and some of the other ideas that I found suggested here.

<%= f.association :sex, :include_blank => true, placeholder: "sex" %>

Related

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

Rails simple_form label for select

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.

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

Disable a Text Box in Ruby on Rails?

I have the code:
<% generate_bullets = Bullet.all %>
<% generate_bullets.shuffle.first(4).each do |t| %>
<%= f.text_field, :bullets, :class => 'text_field disabled' %>
I want to disable a text box using embedded ruby, and am unable to do so. If I could receive any help on the situation I'm facing, it would be very greatly appreciated.
After I disable the text box I want to have a button generate four random ID's from the database table "bullets" and print them on the disabled text box in an array format, and utilize those four printed ID's to post them onto a created page. Any help with that would be even better.
Let me know if I'm reading this right: you're trying to disable the text field from the get-go in the HTML. Is that right?
If so, disabled isn't a class; it's its own attribute.
<%= f.text_field, :bullets, :class => 'text_field', :disabled => true %>
You can also use :readonly => true attribute.
For HAML
= f.text_field :name, :class => "form-control", :readonly => true
For ERB
<%= f.text_field :name, :class => "form-control", :readonly => true %>

Rails simple_form: How to disable error labels?

I'm trying to stop simple_form from adding error labels entirely.
tried the followign CSS:
label.error { display:none; }
but simple_form's JavaScript is setting the following rule when it's generated:
display: block;
Am I missing a config that lets me turn off generation entirely?
This stops them from appearing, which works for now:
label.error {
display: none !important;
visibility:hidden;
}
Give this a try:
<%= f.input :password, error: false %>
Source # lib/simple_form/components/errors.rb
If you want to disable for ALL fields, I believe you'd have to put this on all fields.
You can also disable labels, hints or error or configure the html of any of them:
<%= simple_form_for #user do |f| %>
<%= f.input :username, :label_html => { :class => 'my_class' } %>
<%= f.input :password, :hint => false, :error_html => { :id => "password_error"} %>
<%= f.input :password_confirmation, :label => false %>
<%= f.button :submit %>
<% end %>
For further reference check the link below:
https://github.com/plataformatec/simple_form
If you want to disable error messages on inputs site-wide, you can set this easily in the initialiser config/initializers/simple_form.rb:
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
# Comment this line!
#b.use :error, wrap_with: { tag: :span, class: :error }
end
end
You will no longer see the validation messages beside every input.
In Rails 5 do the following to remove the hint underneath the input field and label from above
<%= f.input :password, required: true, label: false, hint: false %>

Resources