Rails: Custom Text for Rails Form_For Label

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



Leave a reply



Submit