通常情况下,当我添加新用户时,Devise会完美运行。现在,我无法弄清楚为什么会出现这个问题,因为从服务器控制台真的没什么可说的。我用friendly_id btw敲打用户名。我在为配置文件创建的设计之外有第二个users_controller。我怀疑这是Devise不起作用的原因,因为它没有将它用作子类。
以下是我的user.rb
的内容validates :username, :presence => true, :uniqueness => {:case_sensitive => false }
validate :validate_username
validates_uniqueness_of :username, :email
validates_presence_of :email, :username
validates :username,
:presence => true,
:uniqueness => { :case_sensitive => false },
:format => { with: /\A[a-zA-Z]+\z/ },
:length => { in: 4..20 }
validate :password_complexity
=> true
#Changes Devise method to accept both email and username for the login method.
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
if conditions[:username].nil?
where(conditions).first
else
where(username: conditions[:username]).first
end
end
end
#Checks if Password format is correct
def password_complexity
if password.present? && !password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d). /)
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one number"
end
end
#Checks if username exist in the database
def validate_username
if User.where(email: username).exists?
errors.add(:username, :invalid)
end
end
Users_Controller.rb(以防您发现可能导致Devise中断的原因)
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:edit, :update]
before_filter :set_user, only: [:show, :edit, :update]
def index
@users = User.friendly.all.paginate(page: params[:page], :per_page => 45)
end
def show
@user = User.friendly.find(params[:id])
@posts = @user.posts.paginate(:page => params[:page], :per_page => 30)
end
def edit
@user.build_profile if @user.profile.nil?
end
def update
if @user.update(user_params, id: id)
redirect_to user_profile_path(@user)
else
render 'edit'
end
end
def follow
@user = User.find(params[:id])
current_user.follow(@user)
redirect_to :back
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:username, profile_attributes: [:id, :name, :birthday, :sex, :tel, :address, :tagline, :introduction, :avatar])
end
end
application_controller.rb
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?
helper_method :current_user_subscribed?
def current_user_subscribed?
user_signed_in? && current_user.subscribed?
end
private
def after_sign_up_path_for(resource)
home_path(resource)
end
def after_update_path_for(resource)
home_path(resource)
end
def after_sign_in_path_for(resource)
hub_index_path(resource)
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :avatar) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :current_password, :avatar) }
end
end
这是服务器响应
Started GET "/users/sign_up" for 127.0.0.1 at 2015-12-26 19:22:55 -0500
Processing by Devise::RegistrationsController#new as HTML
Rendered users/registrations/new.html.erb within layouts/application (89.1ms)
Completed 200 OK in 626ms (Views: 605.4ms | ActiveRecord: 0.0ms)
Started GET "/users/sign_up?utf8=%E2%9C%93&authenticity_token=6KXAPkbVLgpCOV1mu6j9yHw%2Bif2PkrecoPLqFP08yT644658a74HyN1BAmAUXQ93WPjfSJEwVX6NN5EyfFxyoQ%3D%3D&user%5Busername%5D=Bigjohn&user%5Bavatar%5D=&user%5Bemail%5D=john1002%40hotmail.com&user%5Bpassword%5D=[FILTERED]&user%5Bpassword_confirmation%5D=[FILTERED]&commit=Sign+up" for 127.0.0.1 at 2015-12-26 19:27:11 -0500
Processing by Devise::RegistrationsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6KXAPkbVLgpCOV1mu6j9yHw+if2PkrecoPLqFP08yT644658a74HyN1BAmAUXQ93WPjfSJEwVX6NN5EyfFxyoQ==", "user"=>{"username"=>"Bigjohn", "avatar"=>"", "email"=>"john1002@hotmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Rendered users/registrations/new.html.erb within layouts/application (14.0ms)
Completed 200 OK in 508ms (Views: 507.3ms | ActiveRecord: 0.0ms)
以下是用户的new.html.erb表单
<div class="container-fluid">
<form class="form-horizontal sign-up-form-padding">
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: {multipart: true }) do |f| %>
<%= f.error_notification %>
<div class="col-md-6 registration-form-col-centered">
<div class="sign-up-label">
<h1>Register</h1>
</div>
<fieldset class="form-group">
<label style="font-weight: bold;">Username</label>
<div class="form-control">
<%= f.input :username, label: false %>
</div>
<small class="text-muted">Choose a cool username for your profile</small>
</fieldset>
<fieldset class="form-group">
<label style="font-weight: bold">Upload an Avatar Photo</label>
<div class="form-control">
<%= f.file_field :avatar, required: false %>
</div>
</fieldset>
<fieldset class="form-group">
<label style="font-weight: bold">Email</label>
<div class="form-control">
<%= f.input :email, required: true, autofocus: true, label: false %>
</div>
</fieldset>
<div class="form-group">
<label style="font-weight: bold">Password</label>
<div class="form-control">
<%= f.input :password, required: true, label: false, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
</div>
</div>
<div class="form-group">
<label style="font-weight: bold">Confirm Your Password</label>
<div class="form-control">
<%= f.input :password_confirmation, required: true, label: false %>
</div>
</div>
<div class="form-actions">
<%= f.submit 'Sign up', class: 'btn btn-success btn-lg btn-block' %>
</div>
</div>
<% end %>
</form>
</div>
的routes.rb
devise_for :users
get 'welcome/home', :as => 'home'
get 'welcome/about', :as => 'about'
get 'welcome/contact', :as => 'contact'
get 'welcome/help', :as => 'help'
get 'welcome/blog', :as => 'blog'
get 'welcome/privacy', :as => 'privacy'
get 'welcome/media', :as => 'media'
get 'welcome/developers', :as => 'developers'
#get ':id' => 'users#show', :as => :user_profile
get 'subscription/show'
get 'subscription/new'
get 'subscription/create'
get 'subscription/destroy'
get 'hub/index'
#get ':id/setting' => 'users#edit', :as => :user_setting
#match ':id/setting' => 'users#update', via: [:put, :patch]
root 'welcome#home'
resource :subscription
resources :posts, :only => [:index, :create, :show, :new ]
resources :posts do
put 'like', to: 'link#upvote'
put 'dislike', to: 'link#downvote'
end
resources :users do
member do
get :follow
get :unfollow
end
end