Rails: Custom text for rails form_for label
The second parameter to label
helper will allow you to set custom text.
<%= f.label :name, 'Your Name' %>
Use Ruby on Rails Documentation to look up helper methods.
How do I get Rails to use a custom string label for form
You just need to add it after the :name
field like this
<%= form.label :name, "Username" %>
Ruby On Rails: form_for label and text field
Actually, the name of the functionality you are looking for is placeholder.
You can implement a placeholder for textfield like this,
<%=f.text_field :group_name, placeholder: "Group Name" %>
Rails, insert span tag in form_for label custom text
As most of the Form helpers, you can pass a do/block instead of the name argument:
<%= f.label :address do %>
Address<span class="required">*</span>
<% end %>
Works with link_to
also:
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
Simple_form_for custom label
From documentation:
<%= simple_form_for @user do |f| %>
<%= f.input :username, label: 'Your username please' %>
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
<%= f.input :email, label_html: { class: 'my_class' } %>
<%= f.input :password_confirmation, label: false %>
<%= f.button :submit %>
<% end %>
How to add Label inside correct form group in Ruby on Rails form?
Because you're using bootstrap_form_for
builder, f.text_field
outputs a label by default. You can remove it with skip_label
option. And use your custom label instead.
<%= f.label :ip_address_locator, 'My NEW label', data: { "toggle"=> "tooltip", "placement"=>"bottom", :title => "This is a tooltip text" } %>
<%= f.text_field :ip_address_locator, :skip_label => true %>
However the custom label is now outside of the form group wrapper. If you want to keep the layout unchanged you should explicitly wrap the label and the input:
<% f.form_group do %>
<%= f.label :ip_address_locator, 'My NEW label', data: { "toggle"=> "tooltip", "placement"=>"bottom", :title => "This is a tooltip text" } %>
<%= f.text_field :ip_address_locator, :skip_label => true, wrapper: false %>
<% end %>
As a side note, in an effort to spare you extra work. I'd prefer to have a hint that's always visible if a field requires extra context. Label already has text in it and a tooltip seems redundant. If used in one or two places, I think it's fine. But you would be missing all the benefits of the form builder as well; skipping wrapper and label. You would only get a .form-control
class on your input from the form builder by doing this.
Update for bottstrap_form
< 4.1.0
. No wrapper: false
option.
<style>
.form-group .form-group { margin-bottom: 0; }
</style>
<div class="form-group">
<%= f.label :ip_address_locator, 'My NEW label', class: "control-label col-md-3", data: { "toggle"=> "tooltip", "placement"=>"bottom", :title => "This is a tooltip text" } %>
<div class="col-md-4"
<%= f.text_field :ip_address_locator, layout: :none, :skip_label => true %>
</div>
</div>
Add class to custom form_for label
I guess you can do it in one line:
<%= f.label :name, (@user.errors[:name].blank? 'Name' : 'Name ' + @user.errors[:name].to_sentence) %>
Then:
<% if @user.errors[:name].blank? %>
<%= f.label :name, 'Name' %>
<% else %>
<%= f.label :name, 'Name ' + @user.errors[:name].to_sentence, :class => "some_class" %>
<% end %>
Related Topics
Rails with Paypal Permissions and Paypal Express Checkout
Invalid Multibyte Char (Utf-8) Error, Ruby
Could Not Find a Valid Gem 'Rails' (>= 0) in Any Repository
How to Call Java API from Ruby 1.8 or 1.9
How to Authorize a Service Account for Google Calendar API in Ruby
How to Emit Comments in a Yaml Document Using Psych
Ruby Use Array Tvalues to Index Nested Hash of Hash
How to Fetch Linkedin User Data
Ruby Shoes Gui: Continually Updating Paragraphs
Why Does This Nokogiri Xpath Have a Null Return
Mailer Unable to Access Reset_Token in User Model
How to Use Ruby's Readlines.Grep for Utf-16 Files
Tzinfo-Data Present But Not Seen
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Open_Id_Authentication - "Openidauthentication.Store Is Nil. Using In-Memory Store." Problem
Ruby SASS, Unable to Resolve Dependancies