Setting class and value in simple form (Rails) - ruby-on-rails

I am using simple form in my Rails application, i tried to set value and class to a specific input field just like below
<%= f.input :email, :required => true, :autofocus => true, :value => "Email", :class => "myclass" %>
Html output :
but i could not able to see value and class set in the actual html form.
What am i doing wrong?

I'm not using the simple_form plugin for Rails, but as the documentation says you should use the :input_html attribute for this.
Do the following:
<%= f.input :email, required: true, autofocus: true, input_html: { :value => "Email", class: "myclass" } %>

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

Simple form: set current_user.username as value for commenter

I want to set the name of the current user as the value for the commenter.
when i use: <%= f.input :commenter, :input_html => { :value => '<% current_user.username %>' }, label: false, disabled: true %> I get a syntax error.
Actually, I don't want to display the commentor on adding the comment; only on the listing.
Besides Simple_form, I use Devise.
You're openning the ERB tag twice in the same expression, hence the syntax error. Try :
<%= f.input :commenter, :input_html => { :value => current_user.username }, label: false, disabled: true %>

Ruby on rails text_field confirmation

I have two lines
<%= f.text_field(:email, class: 'new_user_info', autofocus: true, placeholder: 'Email') %>
<%= f.text_field(:email_confirmation, class: 'new_user_info', autofocus: true, placeholder: 'Email Confirmation') %></td>
On http://edgeguides.rubyonrails.org/active_record_validations.html, it states that you can use the
confirmation: true
property with the following format
<%= text_field :person, :email %>
<%= text_field :person, :email_confirmation %>
I need the confirmation to work with the first format that I stated. However, I am unable to do so. When I add the confirmation: true property it is ignored by the validator. Could someone please tell me how to validate the fact that both emails are the same with Ruby on Rails using the first set of text_field tags that I gave?
Thanks for your time
Try using this instead
validates_confirmation_of :email

How to specify placeholders for date_select in rails?

I have haml that uses formtastic to specify date_select input (year,month,day):
= f.input :birthday,
start_year: 1900,
end_year: time.now.year-18,
input_html: { class: 'select2' }
Which renders me 3 select boxes with parent element class as a placeholder:
<select ... placeholder=".span1">
How to specify placeholders? Year;Month;Day would be good enough.
According to Formtastic Documentation:
<%= f.input :publish_at, :as => :date_select, :labels => { :year => "Year", :month => "Month", :day => "Day" } %>
[Edit]
If formtastic-bootstrap gem is used, you probably need to use :placeholder as it is used on the code.
Though, on master the :placeholder option is not implemented, with a TODO comment instead.

set data attribute in grouped_collection_select dynamically in rails 4

I have a form like this in my orders/new page. I have used this to select model location and then choose a model from the location according to what is selected in the above select box.
<%= f.label "SELECT MODEL LOCATION *" %>
<div class="list_number">1</div><%= f.collection_select :location_id, Location.all, :id, :formatted_display, prompt: true, :required => true, :class => 'chosen-select' %>
<%= f.label "SELECT YOUR MODEL *" %>
<div class="list_number">2</div>
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.find_by_location_id(group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
In the grouped_collection_select when I use map to set the data attribute I am getting the following error:
undefined method `map' for #<Performer:0x00000006813958>
How do I solve this error and get the data required?
After trying the below code
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `performers' for [{"data-markup"=>#<BigDecimal:67e80c8,'0.0',9(36)>}]:Array
When I tried the following
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name,Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `merge' for #<Array:0x000000069705a8>
Performer.find_by_location_id(group.id)
returns an instance but map is method for Array.
The fix depends on your business logic (and APP structure)
You can use Performer.where(location_id: group.id).map... if there are many Performers
I think you try to use this helper wrongly. According to documentation you should pass a collection and group_method. group_method - The name of a method which, when called on a member of collection, returns an array of child objects representing the <option> tags.
You pass an array of objects [{'data-markup' => some_value}, ...] as collection and performers as group_method. Rails try to call {'data-markup' => some_value}.performers and this raises an exception.
You should rewrite a collection the following way:
1. it should be array
2. each item of array should have method performers - which return an array of objects under one group item
I hope it will help you
I can't test but it can work (if Location has_many performers or you can implement your own method according to your business logic)
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>

Resources