简体   繁体   English

使用Rails中的引导程序设计登录

[英]Devise login with bootstrap in rails

I'm using devise and the log in page is pretty standard so now i'm trying to use a bootstrap template. 我正在使用devise,并且登录页面非常标准,所以现在我正尝试使用引导程序模板。

Devise code: 设计代码:

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

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

  <% if devise_mapping.rememberable? -%>
    <div class="field">
      <%= f.check_box :remember_me %>
      <%= f.label :remember_me %>
    </div>
  <% end -%>

  <div class="actions">
    <%= f.submit "Log in" %>
  </div>
<% end %>

and the code template i choose: 和我选择的代码模板:

<div class="container">
  <form class="form-signin">
    <h2 class="form-signin-heading">Please sign in</h2>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>
</div> <!-- /container -->

So how do change this template to use my variables like f.email_field, or did i just have to change the id in the input tag ? 那么如何更改此模板以使用诸如f.email_field之类的变量,还是我只需要更改输入标签中的id? i need to use the form_for in the container? 我需要在容器中使用form_for吗? i really don't now how to use this template to work the same way. 我现在真的不怎么使用该模板以相同的方式工作。

OK... so there's a few parts to this question. 好...所以这个问题有几个部分。

First, rather than "changing the template to use your variables", I'd take the reverse approach - change your devise code, bit by bit, until the HTML it produces matches the bootstrap sample. 首先,而不是“更改模板以使用您的变量”,而是采取相反的方法-一点一点地更改您的设计代码,直到它生成的HTML与引导程序示例匹配为止。

Now, in the bootstrap sample, the class attributes are used for styling - they'll need to stay as they are. 现在,在引导程序示例中,将class属性用于样式设置-它们需要保持原样。 The id and for attributes aren't used for styling, and it doesn't matter if those change (ie, devise can pick what it'd like them to be, and you don't have to worry). idfor属性不用于样式设置,并且这些更改是否无关紧要(即,devise可以选择想要的样式,您不必担心)。

So the next step is to go through both code samples, and figure out which bits of the devise code correspond with which bits of the bootstrap code. 因此,下一步是遍历这两个代码样本,并找出设计代码的哪些位与引导程序代码的哪些位相对应。 Then, modify each bit of devise code to more closely match the bootstrap example. 然后,修改设计代码的每一位,使其与引导程序示例更加匹配。 I won't go through the entire code sample, but I'll give a few examples: 我不会遍历整个代码示例,但会给出一些示例:

1 1个

In your devise code, <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 在您的设计代码中, <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> produces a set of form tags in html. <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>用html生成一组表单标签。 So, where are the matching form tags in your bootstrap code? 那么,引导代码中匹配的表单标签在哪里? Well, it's <form class="form-signin"> and </form> . 好吧,这是<form class="form-signin"></form> So, what you need to do is make sure your devise form adds a form-signin class to it's form tag, so it matches the bootstrap template. 因此,您需要做的是确保您的设计表单在表单标签中添加一个form-signin类,以便它与引导程序模板匹配。 the form for doco lets you know that to add a class, you'd do it like this: doco的表单让您知道要添加一个类,您可以这样做:

<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: "form-signin"}) do |f| %>

(note where I've put class: "form-signin" ) (请注意,我将class: "form-signin"放在哪里class: "form-signin"

Alright. 好的。 That's the form tag sorted. 那是对表单标签进行排序。 Next up, labels: 接下来,标签:

2 2

In the devise example, fields are wrapped in <div class="field"> blocks. 在设计示例中,字段包装在<div class="field">块中。 Your bootstrap template doesn't have them at all - so you can just delete them. 您的引导程序模板根本没有它们-因此您可以删除它们。

In devise code, <%= f.label :email %><br /> matches to <label for="inputEmail" class="sr-only">Email address</label> in your bootstrap template. 在设计代码中, <%= f.label :email %><br />与引导模板中的<label for="inputEmail" class="sr-only">Email address</label>匹配。 So how do you modify the devise code to produce html that's the same as the bootstrap template? 那么,如何修改devise代码以生成与引导模板相同的html? Well, you need to remove the <br /> from it, add an 'sr-only' class, and change the label text to 'Email Address'. 好吧,您需要从其中删除<br /> ,添加一个“仅sr”类,并将标签文本更改为“电子邮件地址”。 The documentation will let you know that this can be done with: 该文档将使您知道可以使用以下方法完成此操作:

<%= f.label :email, 'Email address', class: 'sr-only' %>

3 3

Next, input fields. 接下来,输入字段。 <%= f.email_field :email, autofocus: true %> in your devise code corresponds <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> in your bootstrap code. <%= f.email_field :email, autofocus: true %>在您的设计代码中与您的自举程序中的<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>码。 So, you just need to make your devise code add a form-control class. 因此,您只需要使您的设计代码添加一个form-control类即可。 Refer to the doco, or google 'how to add a class to a rails form field', and you'll find it can be done like this: 参考doco或google“如何将类添加到Rails表单字段中”,您会发现可以这样做:

<%= f.email_field :email, autofocus: true, class: 'form-control' %>

Follow that process all the way through, bit by bit, until the html from your devise code has all the classes / modifications needed to make it look like the bootstrap example. 逐步进行该过程,直到设计代码中的html包含使它看起来像引导程序示例所需的所有类/修改。

Final result 最后结果

The final result will look something like this: 最终结果将如下所示:

<div class="container">
  <%= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: "form-signin"}) do |f| %>
    <h2 class="form-signin-heading">Please sign in</h2>
    <%= f.label :email, 'Email Address', class: 'sr-only' %>
    <%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: 'Email Address' %>

    <%= f.label :password, 'Password', class: 'sr-only' %>
    <%= f.password_field :password, autocomplete: "off", class: 'form-control', placeholder: 'Password' %>

    <% if devise_mapping.rememberable? -%>
      <label>
        <%= f.check_box :remember_me, label: false %>
        Remember me
      </label>
    <% end -%>

    <%= f.button "Sign in", class: 'btn btn-lg btn-primary btn-block' %>
  <% end %>
</div> <!-- /container -->

But DON'T just copy/paste that. 但是不要只是复制/粘贴。 Doing that will keep you a beginner forever. 这样做将使您永远成为新手。 Do it bit by bit yourself until you get it right. 自己一点一点地做,直到正确为止。 Use that final result just as a reference if you really get stuck. 如果您确实遇到困难,可以将最终结果用作参考。

Good luck! 祝好运!

You can add class and id with ruby like this: 您可以使用ruby添加类和ID,如下所示:

<%= f.label :email,'Email address', class: 'sr-only' %>
<%= f.email_field :email, autofocus: true, class: 'form-control', id: 'inputEmail' %>

Bootstrap uses checkbox inputs nested within labels. Bootstrap使用嵌套在标签内的复选框输入。 http://getbootstrap.com/css/#forms You can get around that by just adding an html label tag around your rails style checkbox. http://getbootstrap.com/css/#forms只需在rails样式复选框周围添加html标签即可解决此问题。

<% if devise_mapping.rememberable? -%>
    <div class="checkbox">
        <label><%= f.check_box :remember_me %> Remember me</label>
    </div>
<% end -%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM