How to create a bootstrap paragraph 'help-block' for a input field in Rails 4

871 Views Asked by At

I´m using Bootstrap with Rail 4 to create a new form for a Model and I want to create a form_group like this:

<label for="model_name">Name</label>
<input class="form-control" id="model_name" name="model[name]" placeholder="Your name" type="text">
<p class="help-inline" id="model_name_inline">Help inline</p>

In my view I do this:

<%= form_for @model, url: {action: "create"} do |f| %>

<%= f.label :name %>
<%= f.text_field :name, placeholder: "Your name" %>
<%= content_tag :p, "Help inline", class: "help-inline", id: :name %>

<% end %>

But I can´t do the "id" of the content_tag be like the label and input (in this case, 'model_name') adding "inline" to the id

How I can do this? Is there any kind of content_field for custom fields of a form? :-)

Thank you in advance.

UPDATE: Thanks to the comment I´ve updated the question with the correct id

1

There are 1 best solutions below

0
On BEST ANSWER

I would extract that into a helper. For example

module ApplicationHelper
  def inline_help(help_text, object_name, method)
    content_tag :p, help_text, class: "help-inline", id: "#{object_name}_#{method}_inline"
  end
end

You can then use it within your form like this

<%= form_for @model, url: {action: "create"} do |f| %>

<%= f.label :name %>
<%= f.text_field :name, placeholder: "Your name" %>
<%= inline_help 'Help inline', f.object_name, :name %>

<% end %>

Hope this helps.