我正在尝试在我的主页上设置登录表单。我设法按照Wiki
进行操作除非登录信息不正确,否则会呈现/devise/session/new.html.erb
。
我不希望这样。我希望我的用户被重定向到root_url
WITH 错误消息中的错误。
我能够覆盖registrations_controller.rb
以获取其他功能,但覆盖sessions_controller.rb
会给我带来很多麻烦。
我想改变做我想做的事情是什么?如果登录正确,我仍然希望将用户重定向到after_sign_in_path
。
原始控制器为here
到目前为止,我知道我必须在我的routes.rb
devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions" }
我还设置了controller/sessions_controller.rb
class SessionsController < Devise::SessionsController
# GET /resource/sign_in
def new
resource = build_resource
clean_up_passwords(resource)
respond_with_navigational(resource, stub_options(resource)){ render_with_scope :new }
end
end
我觉得我必须改变render_with_scope :new
但是......怎么样?
在该版本中,我收到错误undefined method 'users_url' for #<SessionsController:0x5072c88>
好吧,我会等你的宝贵帮助
PS:post帮助我处理订阅错误。也许这对登录错误有帮助吗?
====编辑====
根据第一个答案建议,我还添加到initializers/devise.rb
:
config.warden do |manager|
manager.failure_app = CustomFailure
end
当用户未登录并尝试访问“受限”区域时,他会被重定向到root_url
但是当登录出错时,我现在出现以下错误:
The action 'devise/sessions#new' could not be found for Devise::SessionsController
(PS:我删除了使用会话控制器执行的所有操作,如果成功则登录工作)
===编辑2 ===
在Wiki之后,重定向工作完美但是我没有任何错误通知。
我正在显示警告/通知flash消息,如果这改变了什么
<% flash.each do |name, msg| %>
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :p, message, :id => "flash_#{name}" %>
<% end %>
<% else %>
<%= content_tag :p, msg, :id => "flash_#{name}" %>
<% end %>
<% end %>
===最终更新===
接受的答案有效。只是不要忘记显示flash alerts
答案 0 :(得分:14)
您必须覆盖自定义故障设计类。
在lib / custom_failure.rb
下添加此自定义失败类class CustomFailure < Devise::FailureApp
def respond
if http_auth?
http_auth
elsif warden_options[:recall]
recall
else
redirect
end
end
def redirect
store_location!
flash[:alert] = i18n_message unless flash[:notice]
redirect_to "/"
end
def recall
env["PATH_INFO"] = attempted_path
flash.now[:alert] = i18n_message(:invalid)
self.response = recall_controller.action(warden_options[:recall]).call(env)
end
protected
def i18n_message(default = nil)
message = warden.message || warden_options[:message] || default || :unauthenticated
if message.is_a?(Symbol)
I18n.t(:"#{scope}.#{message}", :resource_name => scope,
:scope => "devise.failure", :default => [message, message.to_s])
else
message.to_s
end
end
def warden_options
env['warden.options']
end
def warden
env['warden']
end
def recall_controller
"#{params[:controller].camelize}Controller".constantize
end
def attempted_path
warden_options[:attempted_path]
end
def store_location!
session[:"#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
end
def attempted_path
warden_options[:attempted_path]
end
def http_auth?
!Devise.navigational_formats.include?(request_format) || (request.xhr? && Devise.http_authenticatable_on_xhr)
end
end
在config / application.rb下写下
config.autoload_paths += %W(#{config.root}/lib)
_________________编辑__________________
你说当'注册出错'时,这意味着控件应该是registrations_controller create
方法。我想有些东西肯定是错的。尝试添加这些路线。
devise_for :users
devise_scope :user do
root :to => "devise/sessions#new"
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end
________________编辑2 ________________
在views / devise / registrations / new.html.erb
中添加此内容<%= devise_error_messages! %>
答案 1 :(得分:3)
如果在覆盖设计会话控制器时遇到错误undefined method 'users_url' for #<SessionsController:0x5072c88>
,则可以提供如下路径名称。您错过的重要补充是, :as => :users
。
devise_scope :user do
root :to => "devise/sessions#new", :as => :users
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end
上述路由名称可能与现有或未来路由冲突。作为替代方案,我发现这种设置往往效果很好:
devise_for :users,
:path_names => { :sign_out => 'logout',
:sign_in => 'login',
:sign_up => 'register' },
:controllers => { :sessions => 'users/sessions' } do
# Sessions
post '/login' => 'users/sessions#create', :as => :user_session
get '/login' => 'users/sessions#new', :as => :new_user_session
get '/logout' => 'users/sessions#destroy', :as => :destroy_user_session
# Passwords
post '/password' => 'devise/passwords#create', :as => :user_password
put '/password' => 'devise/passwords#update'
get '/password/new' => 'devise/passwords#new', :as => :new_user_password
get '/password/edit' => 'devise/passwords#edit', :as => :edit_user_password
# Registrations
post '/register' => 'devise/registrations#create', :as => :user_registration
get '/register' => 'devise/registrations#new', :as => :new_user_registration
get '/account' => 'devise/registrations#edit', :as => :edit_user_registration
put '/account' => 'devise/registrations#update'
delete '/account' => 'devise/registrations#destroy'
end
答案 2 :(得分:0)
调试提示尝试删除任何记录(用户将是连贯的);如果你去show动作,它不是一个设计配置问题。对于这种情况,请继续(其他案例在这里回答)
我无法看到您在视图中调用根javascript文件的位置。
我在这个问题上几次使用不同的布局设置不同的javascript设置。无论你的标题是什么,通话
<%= javascript_include_tag 'my-differntly-fangled-application' %>
需要有相关的清单文件包括
//= require jquery
//= require jquery_ujs