我正在尝试在我的主页上设置登录表单。我按照 Wiki 设法做到了
除了,如果登录信息不正确,/devise/session/new.html.erb
被渲染。
我不想要那个。我希望我的用户被重定向到 root_url
与 Flash 消息中的错误。
我能够覆盖 registrations_controller.rb
对于另一个功能,但覆盖 sessions_controller.rb
给我带来很多麻烦。
我应该改变什么来做我想做的事?我仍然希望用户被重定向到 after_sign_in_path
如果登录正确。
原 Controller 为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:我删除了我对 session Controller 所做的一切,如果成功登录就可以了)
=== 编辑 2 ===
关注此 Wiki重定向工作完美 但是 我没有任何错误通知。
如果发生任何变化,我将显示警报/通知闪烁消息
<% 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
最佳答案
您必须覆盖您的自定义故障设计类。
在 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! %>
关于ruby-on-rails - rails 和设计 : Override SessionsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6110047/