Devise Customization issue with rails-bootstrap-fo

2019-06-14 09:06发布

问题:

I am using rails-bootstrap-forms gem with devise. My previous devise/registrations/new.html.erb code was

    <h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

Generating HTML

<h2>Sign up</h2>

<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="9KUX1yFTJ2VKU7zcHVrxr9I9WabU4rHmzHLGL8g9UgY=" /></div>


  <div><label for="user_email">Email</label><br />
  <input autofocus="autofocus" id="user_email" name="user[email]" type="email" value="" /></div>

  <div><label for="user_password">Password</label> <i>(8 characters minimum)</i><br />
    <input autocomplete="off" id="user_password" name="user[password]" type="password" /></div>

  <div><label for="user_password_confirmation">Password confirmation</label><br />
    <input autocomplete="off" id="user_password_confirmation" name="user[password_confirmation]" type="password" /></div>

  <div><input name="commit" type="submit" value="Sign up" /></div>
</form>
  <a href="/users/sign_in">Log in</a><br />

but now I have edited this code to work with gem rails-bootstrap-forms My new code is

<div class="col-md-4">

</div>

  <div class="col-md-4">

        <h2>Sign up</h2>

        <%= bootstrap_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
          <%= devise_error_messages! %>


            <%= f.email_field :email, autofocus: true %>     



            <%= f.label :password do %>
            <% if @validatable %>
            <em>(<%= @minimum_password_length %> characters minimum)</em>
            <% end %>
           <% end %>
            <%= f.password_field :password, autocomplete: "off" %>


            <%= f.password_field :password_confirmation, autocomplete: "off" %>


          <div class="actions">
            <%= f.submit "Sign up" %>
          </div>
        <% end %>

        <%= render "devise/shared/links" %>

    </div> 

  <div class="col-md-4">
  </div>
</div>

HTML view is:-

<div class="row">

<div class="col-md-4">

</div>

  <div class="col-md-4">

        <h2>Sign up</h2>

        <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="opOcH7N0F5S+HQYAD/xqRaqoEOctuElUNBm8BsD/bTM=" /></div>



            <div class="form-group"><label class="control-label" for="user_email">Email</label><input autofocus="autofocus" class="form-control" id="user_email" name="user[email]" type="email" value="" /></div>     



            <label for="user_password">
            <em>(8 characters minimum)</em>
</label>            <div class="form-group"><label class="control-label" for="user_password">Password</label><input autocomplete="off" class="form-control" id="user_password" name="user[password]" type="password" /></div>


            <div class="form-group"><label class="control-label" for="user_password_confirmation">Password confirmation</label><input autocomplete="off" class="form-control" id="user_password_confirmation" name="user[password_confirmation]" type="password" /></div>


          <div class="actions">
            <input class="btn btn-default" name="commit" type="submit" value="Sign up" />
          </div>
</form>
          <a href="/users/sign_in">Log in</a><br />







    </div> 

  <div class="col-md-4">
</div>
</div>

My problem is how I can include the line printing (x characters minimum) in label: password. The above line is printed in next line of label password i want to print it with label password

I want HTML output as

Password (8 characters minimum)

But its showing

 Password
 (8 characters minimum)

回答1:

This is your code

<%= f.label :password do %>
  <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
  <% end %>
<% end %>

<%= f.password_field :password, autocomplete: "off" %>

f.password_field generates a label tag and a password field so your code will generate 2 label tags and one password field.

Replace the above code with this

<% label = @validatable ? "Password <em>(#{@minimum_password_length} characters minimum)</em>".html_safe : "Password" %>
<%= f.password_field :password, autocomplete: "off", label: label %>

This will generate one label tag and one input field as your original code.

For more information, look at the Labels section.