Add login sign-up function for app Ruby on Rails

Source: Internet
Author: User
Tags ruby on rails
(1) Add gem

gem ‘design’
 

(2) Add design configuration file

/ workspace / shop: $ rails generate devise: install User
      create config / initializers / devise.rb
      create config / locales / devise.en.yml
======================================================= ===============================

Some setup you must do manually if you have n‘t yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config / environments / development.rb:

       config.action_mailer.default_url_options = {host: ‘localhost’, port: 3000}

     In production,: host should be set to the actual host of your application.

  2. Ensure you have defined root_url to * something * in your config / routes.rb.
     For example:

       root to: "home # index"

  3. Ensure you have flash messages in app / views / layouts / application.html.erb.
     For example:

       <p class = "notice"> <% = notice%> </ p>
       <p class = "alert"> <% = alert%> </ p>

  4. If you are deploying on Heroku with Rails 3.2 only, you may want to set:

       config.assets.initialize_on_precompile = false

     On config / application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

  5. You can copy Devise views (for customization) to your app by running:

       rails g devise: views

======================================================= ===============================
(3) Add a user model

/ workspace / shop: $ rails generate devise User
      invoke active_record
      create db / migrate / 20151026095446_devise_create_users.rb
      create app / models / user.rb
      invoke rspec
      create spec / models / user_spec.rb
      insert app / models / user.rb
       route devise_for: users
(4) Install the design view file

/ workspace / shop: $ rails g devise: views
      invoke Devise :: Generators :: SharedViewsGenerator
      create app / views / devise / shared
      create app / views / devise / shared / _links.html.erb
      invoke form_for
      create app / views / devise / confirmations
      create app / views / devise / confirmations / new.html.erb
      create app / views / devise / passwords
      create app / views / devise / passwords / edit.html.erb
      create app / views / devise / passwords / new.html.erb
      create app / views / devise / registrations
      create app / views / devise / registrations / edit.html.erb
      create app / views / devise / registrations / new.html.erb
      create app / views / devise / sessions
      create app / views / devise / sessions / new.html.erb
      create app / views / devise / unlocks
      create app / views / devise / unlocks / new.html.erb
      invoke erb
      create app / views / devise / mailer
      create app / views / devise / mailer / confirmation_instructions.html.erb
      create app / views / devise / mailer / reset_password_instructions.html.erb
      create app / views / devise / mailer / unlock_instructions.html.erb
(5) Generate the following table

/ workspace / shop: $ rake db: migrate
== 20151026095446 DeviseCreateUsers: migrating ====================================
-create_table (: users)
   -> 0.0312s
-add_index (: users,: email, (: unique => true))
   -> 0.0004s
-add_index (: users,: reset_password_token, (: unique => true))
   -> 0.0004s
== 20151026095446 DeviseCreateUsers: migrated (0.0322s) ==========================
(6) Added login to the navigation bar app / views / layouts / application.html.erb

          <ul class = "nav navbar-nav navbar-right">
            <% if user_signed_in?%>
              <li> <% = link_to current_user.email, profile_path%> </ li>
              <li> <% = link_to "Quit", destroy_user_session_path, method:: delete%> </ li>
            <% else%>
              <li> <% = link_to "login", new_user_session_path%> </ li>
              <li> <% = link_to "Registration", new_user_registration_path%> </ li>
            <% end%>
          </ ul>
(7) Modify the style of the login page

<div class = "row">
  <div class = "col-md-6">
    <% = form_for (resource, as: resource_name, url: session_path (resource_name)) do | f |%>
      <div class = "form-group">
        <% = f.label: email, class: "control-label"%>
        <% = f.email_field: email, autofocus: true, class: "form-control"%>
      </ div>

      <div class = "form-group">
        <% = f.label: password, class: "control-label"%>
        <% = f.password_field: password, autocomplete: "off", class: "form-control"%>
      </ div>

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

      <div class = "actions">
        <% = f.submit "Login", class: "btn btn-primary"%>
        <% = link_to "Forgot Password", new_password_path (resource_name), class: "btn btn-link"%>
      </ div>
    <% end%>
  </ div>
</ div>
(8) In order to protect our methods, add a login verification app / controllers / application_controller.rb before each method

class ApplicationController <ActionController :: Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use: null_session instead.
  protect_from_forgery with:: exception

  before_action: authentica
te_user!
end
(9) For some pages that can be published without login, add a statement that skips verification in the controller

class ProductsController <ApplicationController
   skip_before_action: authenticate_user !, only: [: index,: show]
 

Add login registration function for the application ruby on rails
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.