加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails&Devise:覆盖SessionsController

发布时间:2020-12-16 20:24:02 所属栏目:百科 来源:网络整理
导读:我试图在主页上设置一个登录表单.我按照 Wiki做了这件事 除非登录信息不正确,否则/devise/session/new.html.erb将被渲染. 我不想要我希望我的用户被重定向到root_url与闪存消息中的错误. 我能够覆盖registrations_controller.rb的另一个功能,但覆盖sessions_
我试图在主页上设置一个登录表单.我按照 Wiki做了这件事

除非登录信息不正确,否则/devise/session/new.html.erb将被渲染.
我不想要我希望我的用户被重定向到root_url与闪存消息中的错误.

我能够覆盖registrations_controller.rb的另一个功能,但覆盖sessions_controller.rb给了我很多麻烦.

我想换什么来做我想要的?我仍然希望将用户重定向到after_sign_in_path,如果登录正确.
原控制器为here

到目前为止,我知道我必须在我的路线

devise_for:users,:controllers => {:registrations => “注册”,: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:新的但…如何?
使用该版本,我得到#< SessionsController:0x5072c88>的错误undefined方法’users_url’

嗯,我会等待你的宝贵帮助

PS:post帮助我很多处理订阅错误.也许这将有助于登录错误?

====编辑====

遵循第一个回答建议,我还添加了初始化器/ 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.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 %>

=== FINAL UPDATE ===

接受的答案有效.只是不要忘记显示闪光警报

解决方法

您必须覆盖您的自定义失败设计类.

在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创建方法中.我猜,那里的东西一定是错的.尝试添加这些路由.

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 ________________

在视图/ devise / registrations / new.html.erb中添加

<%= devise_error_messages! %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读