Rails设计登录不工作

时间:2015-11-20 08:10:10

标签: ruby-on-rails ruby login devise

我有一个问题 - 最近我用devise gem创建了User。可悲的是," sign_in"按钮不能正常工作 - 访问sin_in页面并点击登录按钮后没有任何反应!如果我输入用于登录的电子邮件和密码并不重要。我检查了日志,点击注册按钮后,日志或运行服务器的终端上没有任何变化!所以我猜它不会发送任何请求?但问题发生在我第一次访问登录页面后,刷新后所有工作都按预期工作。请帮助我,我不知道是什么原因导致这种奇怪的行为。

编辑:奇怪的是,我注意到只有在从我的主页面登录页面后才会出现问题 - 如果我只是通过键入地址而不是点击浏览器链接中的单词继续登录页面就可以了。但是两个动作之后的地址是相同的,所以我不知道......有我欢迎的观点:

<div id="homepage">
  <header class="cf">
    <h1 class="logo"><%= link_to "E-LEARN", "#" %></h1>
    <nav>
      <ul>
        <li><%= link_to "Watch guides", root_path %></li>
        <% unless user_signed_in? %>
          <li><%= link_to "Log In", new_user_session_path %></li>
        <% else %>
          <li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>
          <li><%= link_to "Settings", edit_user_registration_path %></li>
          <li><%= link_to "My Account", current_user %></li>
        <% end %>
        <li><%= link_to "Add tutorial", "#" %></li>
      </ul>
    </nav>
  </header>
  <div class="wrapper-skinny" id="welc">
    <h1>Learn programming from others, or share your experience with novices.</h1>
  </div>
  <div class="buttons">
    <%= link_to "Learn something", new_user_registration_path , class: "button button-highlight" %>
    <%= link_to "Share experience", "#", class: "button button-dark"%>
  </div>
</div>


有我的档案:
应用程序控制器:

 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_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :description, :email, :password, :password_confirmation, :current_password) }
  end
end

迁移:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :guides
  validates :username, presence: true, length: { minimum: 5 }
end

路线:

Rails.application.routes.draw do

  root to: 'welcome#index'
  devise_for :users

  resources :users, only: [:show, :index] do
    resources :guides
  end

end

和gemfile:

source 'https://rubygems.org'

gem 'rails', '4.2.2'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'sqlite3'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt', '~> 3.1.7'
gem 'devise', '~> 3.5', '>= 3.5.2'
gem 'paperclip', '~> 4.3', '>= 4.3.1'

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg', '0.17.1'
  gem 'rails_12factor'
end

路线:

                 Prefix Verb   URI Pattern                               Controller#Action
                    root GET    /                                         welcome#index
        new_user_session GET    /users/sign_in(.:format)                  devise/sessions#new
            user_session POST   /users/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                 devise/sessions#destroy
           user_password POST   /users/password(.:format)                 devise/passwords#create
       new_user_password GET    /users/password/new(.:format)             devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)            devise/passwords#edit
                         PATCH  /users/password(.:format)                 devise/passwords#update
                         PUT    /users/password(.:format)                 devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                   devise/registrations#cancel
       user_registration POST   /users(.:format)                          devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                  devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                     devise/registrations#edit
                         PATCH  /users(.:format)                          devise/registrations#update
                         PUT    /users(.:format)                          devise/registrations#update
                         DELETE /users(.:format)                          devise/registrations#destroy
             user_guides GET    /users/:user_id/guides(.:format)          guides#index
                         POST   /users/:user_id/guides(.:format)          guides#create
          new_user_guide GET    /users/:user_id/guides/new(.:format)      guides#new
         edit_user_guide GET    /users/:user_id/guides/:id/edit(.:format) guides#edit
              user_guide GET    /users/:user_id/guides/:id(.:format)      guides#show
                         PATCH  /users/:user_id/guides/:id(.:format)      guides#update
                         PUT    /users/:user_id/guides/:id(.:format)      guides#update
                         DELETE /users/:user_id/guides/:id(.:format)      guides#destroy
                   users GET    /users(.:format)                          users#index
                    user GET    /users/:id(.:format)                      users#show

和我的登录视图(devise / sessions / new.html.erb):

<%= render 'shared/header' %>

<div class="banner" >
</div>

<div class="wrapper-skinny">
  <h2>Log in</h2>

  <%= 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" id="checkbox">
        <%= f.check_box :remember_me %>
        <%= f.label :remember_me %>
      </div></div><br>
    <% end -%>

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

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

2 个答案:

答案 0 :(得分:2)

看起来像turbolinks问题。

  1. 从Gemfile中删除turbolinks gem
  2. 运行捆绑安装
  3. 在app / views / layouts / application.html.haml中将turbolinks设置为false,如下所示:
  4. = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => false

    = javascript_include_tag 'application', 'data-turbolinks-track' => false

    或删除它们。

    1. 从app / assets / javascripts / application.js
    2. 中删除//= require turbolinks
    3. 重新启动服务器

答案 1 :(得分:1)

@JakubKopyś以下是从turbolinks中排除单链接而不删除gem的好方法(来自turbolinks github repo):

“默认情况下,所有内部HTML链接都将通过Turbolinks进行汇总,但您可以通过使用data-no-turbolink标记链接或其父容器来选择退出。例如,如果您使用data-no-turbolink标记div ,然后该div内的所有链接都将被视为常规链接。如果您标记正文,则整个页面上的每个链接都将被视为常规链接。“

<div id="some-div" data-no-turbolink> <a href="/">Home (without Turbolinks)</a> </div>