Rails Login with Twitter Bootstrap Navbar (w/ Devi

2019-05-27 12:07发布

问题:

I am trying to use Twitter's Bootstrap (http://twitter.github.com/bootstrap/index.html) with Ruby on Rails (v 3.2.8 with Ruby 1.9.3). I am using Devise for authentication.

Twitter has a nice sign-in form in their navbar. (see http://twitter.github.com/bootstrap/examples/hero.html?)

How would I set up my Rails application to use that login? I can set Rails up to login from its own login page, but how do I use the Navbar login? What would be the controller/model structure?

I found this on github, but it does not implement the navbar signup (https://github.com/RailsApps/rails3-bootstrap-devise-cancan/).

Thanks

UPDATE:

Found the answer : https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

回答1:

You can simply run rails g devise:views and customize the form to display correctly inside the nabvar. The you render it with a render 'devise/sessions/new' inside any of your layouts. For more detail look here.



回答2:

The code from the example has the following after the tag.

        <form class="navbar-form pull-right">
          <input class="span2" placeholder="Email" type="text">
          <input class="span2" placeholder="Password" type="password">
          <button type="submit" class="btn">Sign in</button>
        </form>

I would use this code, but change the form's action and input names to that of my login form.

Just view the source code of your login form and change the necessary values. You could also add in some code similar to below so that it will not show the login form if they are already authenticated.

<% if not current_user %>
//login form
<% else %>
//welcome the user and provide logout option
<% end %>