問題描述
Rails 設計登錄不工作 (Rails devise sign in not working)
我有一個問題 ‑ 最近我使用 devise gem 創建了用戶。可悲的是,“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>
參考解法
方法 1:
Looks like turbolinks problem.
- Remove turbolinks gem from your Gemfile
- Run bundle install
- In app/views/layouts/application.html.haml set turbolinks to
false
like that:
= stylesheet_link_tag 'application', media: 'all', 'data‑turbolinks‑track' => false
= javascript_include_tag 'application', 'data‑turbolinks‑track' => false
or remove them.
- remove
//= require turbolinks
from app/assets/javascripts/application.js - Restart server
方法 2:
<p>@JakubKopyś Here is nice way to exclude single link from turbolinks without removing gem (from turbolinks github repo):</p>"By default, all internal HTML links will be funneled through Turbolinks, but you can opt out by marking links or their parent container with data‑no‑turbolink. For example, if you mark a div with data‑no‑turbolink, then all links inside of that div will be treated as regular links. If you mark the body, every link on that entire page will be treated as regular links."
<div id="some‑div" data‑no‑turbolink> <a href="/">Home (without Turbolinks)</a> </div>
(by Jakub Kopyś、weezing、weezing)