Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么Desive不在登录页面上显示身份验证错误?_Ruby On Rails_Ruby_Ruby On Rails 4_Devise - Fatal编程技术网

Ruby on rails 为什么Desive不在登录页面上显示身份验证错误?

Ruby on rails 为什么Desive不在登录页面上显示身份验证错误?,ruby-on-rails,ruby,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,我使用的是rails 4.2 我有一个名为designe_helper.rb的助手文件 module DeviseHelper def devise_error_messages! return "" if resource.errors.empty? messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join sentence = I18n.t("e

我使用的是rails 4.2

我有一个名为designe_helper.rb的助手文件

module DeviseHelper
    def devise_error_messages!
        return "" if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t("errors.messages.not_saved",
                  count: resource.errors.count,
                  resource: resource.class.model_name.human.downcase)

     html = <<-HTML
     <div class="row">
     <div class="large-12 columns">
        <div data-alert class="alert-box alert radius">
          <h4>#{sentence}</h4>
          <ul>#{messages}</ul>
        </div>
     </div>
     </div>
     HTML
     html.html_safe
  end
end
模块设备帮助器
def设计错误消息!
如果resource.errors.empty返回“”?
messages=resource.errors.full_messages.map{| msg | content_标记(:li,msg)}.join
语句=I18n.t(“错误.消息.未保存”,
计数:resource.errors.count,
资源:resource.class.model_name.human.downcase)

html=当您运行
designe:install
(或类似程序)时,它必须创建带有特定代码行的视图来显示这些消息

如果您查看,它们会解释您可以在视图中添加
,以显示这些错误消息(这与HTML中的某个地方有一个通用的
没有什么不同…)


与会话相关的视图很可能不包含这行代码。

带有空白/错误字段的登录不会触发(您的)模型验证,因此不会显示您的验证错误

如果使用
byebug
(例如在视图的第一行)进行调试,您会注意到

resource.errors.count # => 0
flash # => ....@flashes={"alert"=>"Invalid email or password."}
Desive使用此登录上下文特有的特定登录错误消息填充“警报闪存”

为什么看不到所有模型验证错误消息?因为这是没有意义的:假设您的模型有一个带有
的强制
:gender
属性,该属性验证:gender
的存在。如果添加了正常的模型错误,那么当您的用户尝试使用错误的登录名登录时,您还会在错误列表中看到“性别不能为空”:oops:

设计错误消息
是一种特定的设计方法,旨在显示这些特定错误。您可以将其视为对用于登录的字段(以及在designe配置文件中定义的字段)的部分验证

解决方法:

如果确实要显示所有错误消息,可以明确运行验证:

的开头设计错误消息

resource.validate # It will generate errors and populate `resource.errors`

我相信它不应该干扰其他已经很好运行的操作(注册等)

对于更新版本的Rails/Desive,我建议:

代替
执行以下操作:

      <% if flash[:alert] %>
        <%= flash[:alert] %>
      <% end %>
我喜欢这样。 不过HTML部分有点原创

我不想单独显示有关身份验证的错误消息。 所以,我用这种方式

module DeviseHelper
  def devise_error_messages!

    flash_alerts = []

    if !flash.empty?
      flash_alerts.push(flash[:error]) if flash[:error]
      flash_alerts.push(flash[:alert]) if flash[:alert]
      flash_alerts.push(flash[:notice]) if flash[:notice]
      sentence = I18n.t("devise.failure.invalid") #=> "Invalid email or password."
    else
      sentence = I18n.t("errors.messages.not_saved",
                        count: resource.errors.count,
                        resource: resource.class.model_name.human.downcase)
    end

    return "" if resource.errors.empty? && flash_alerts.empty?

    errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages
    messages = errors.map { |msg| content_tag(:li, msg) }.join

    html = <<-HTML
    <div class="alert alert-danger">
      <div id="error_explanation">
        <strong>#{sentence}</strong>
        <ul class="m-b-0 p-l-30 ">#{messages}</ul>
      </div>
    </div>
    HTML

    html.html_safe
  end
end
模块设备帮助器
def设计错误消息!
闪光警报=[]
如果!闪光,空的?
flash_警报。如果flash[:error],则推送(flash[:error])
flash_alert.push(flash[:alert]),如果flash[:alert]
flash_警报。如果flash[:notice],则推送(flash[:notice])
句子=I18n.t(“设计.失败.无效”)#=>“无效的电子邮件或密码。”
其他的
语句=I18n.t(“错误.消息.未保存”,
计数:resource.errors.count,
资源:resource.class.model_name.human.downcase)
结束
如果resource.errors.empty返回“”&&flash_.empty?
errors=resource.errors.empty?flash_警报:resource.errors.full_消息
messages=errors.map{| msg | content_标记(:li,msg)}.join

html=您能为您的会话提供控制器吗?我想知道你的
create
动作是什么样子的like@TimKos请参见上面的编辑。我没有更改创建操作,所以它应该只是设计默认值。等等,你的意思是没有呈现错误,还是错误消息的自定义在这些会话视图上不起作用?@CyrilDD在我发布的代码中,我目前在我的应用程序中有所有这些,包括。。。在application.html.erb和我的会话视图中。当我提交一个没有填写任何字段的登录表单时,我不会得到我期望从中得到的自定义错误消息。相反,我得到了application.html.erb中列出的定制,这不是我所期望的,因为密码或注册表的视图都没有这样做。我的问题是,为什么只有在会话中才会发生这种情况?结果是,这些模型没有经过会话的验证#create!查看我的另一个答案。我必须礼貌地表示不同意,一旦您将
flash.each{…}
添加到application.html.erb,这些操作应该在所有呈现的视图中都可用。不需要单独指定。哦,对不起,我不知道您已经在application.html.erb中添加了这些代码行。您是否尝试使用调试器/打印
设计错误消息的输出以确保有任何东西可以渲染?不,我对会话的看法是肯定的。好吧,我相信我理解Desive为什么做出这个选择。实际上,如果你验证模型,它会添加所有可能的错误,因为它无法从数据库中检索用户(例如:如果你有
validate\u presence\u:gender
,并且在登录失败后进行验证,那么,它还会显示一个错误
:gender=>“不能为空”
,这在登录上下文中是毫无意义的^^"谢谢你的仔细检查。这就是我真正想知道的,为什么它没有任何错误,为什么debs会这样设计。@Henry可能是对的,那些错误消息保存在
闪存中,而不是作为
对象
验证错误。在
资源上运行
验证
将不起作用对于错误的登录身份验证…密码也将在未成功登录尝试后清除。我的一些应用程序没有此问题,但我无法找出原因
      <% if flash[:alert] %>
        <%= flash[:alert] %>
      <% end %>
RSpec.feature 'Sign In', :type => :feature do

  describe "correct error message w/ wrong password" do
    before :each do
      @user = create(:user)
      @user.confirm

      visit new_user_session_path
      fill_in "user_email", with: @user.email
      fill_in "user_password", with: "wrongpassword"
      click_button "Log in"
    end

    it "tells user on page 'Invalid Email or password'" do
      expect(page).to have_text("Invalid Email or password")
    end
  end

end
module DeviseHelper
  def devise_error_messages!

    flash_alerts = []

    if !flash.empty?
      flash_alerts.push(flash[:error]) if flash[:error]
      flash_alerts.push(flash[:alert]) if flash[:alert]
      flash_alerts.push(flash[:notice]) if flash[:notice]
      sentence = I18n.t("devise.failure.invalid") #=> "Invalid email or password."
    else
      sentence = I18n.t("errors.messages.not_saved",
                        count: resource.errors.count,
                        resource: resource.class.model_name.human.downcase)
    end

    return "" if resource.errors.empty? && flash_alerts.empty?

    errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages
    messages = errors.map { |msg| content_tag(:li, msg) }.join

    html = <<-HTML
    <div class="alert alert-danger">
      <div id="error_explanation">
        <strong>#{sentence}</strong>
        <ul class="m-b-0 p-l-30 ">#{messages}</ul>
      </div>
    </div>
    HTML

    html.html_safe
  end
end