Ruby-on-rails 当我在同一页面中登录并注册表单时如何显示设计错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6661481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:34:43  来源:igfitidea点击:

how to display devise error messages when i'm putting the login in and sign up forms in same page

ruby-on-railsrubyruby-on-rails-3devise

提问by gal

I'm using devise and I put the login and sign up forms in the same page, now when I write invalid login details or don't fill required input data at the sign up form.

我正在使用设计并将登录和注册表单放在同一页面中,现在当我写无效的登录详细信息或未在注册表单中填写所需的输入数据时。

I'm redirecting to the /userspage if I'm trying to register or to the /users/sign_inif I try to login with the errors i made...

/users如果我尝试注册或/users/sign_in尝试登录时出现错误,我将重定向到该页面...

I want to stay in the same page and show the errors in the same page.

我想留在同一页面并在同一页面中显示错误。

how can I do it?

我该怎么做?

thanks you very much, I need a quick help :)

非常感谢你,我需要一个快速的帮助:)

回答by Lucas

I found the solution to this problem on StackOverFlow some time ago. Here's what worked for me

前段时间我在 StackOverFlow 上找到了解决这个问题的方法。这对我有用

# In application.html.erb
<% flash.each do |name, msg| %>

  # New code (allow for flash elements to be arrays)
  <% if msg.class == Array %>
    <% msg.each do |message| %>
      <%= content_tag :div, message, :id => "flash_#{name}" %>
    <% end %>
  <% else %>

    # old code
    <%= content_tag :div, msg, :id => "flash_#{name}" %>

  <% end %> #don't forget the extra end
<% end %>

and

# Wherever you want Devise's error messages to be handled like 
# your other error messages
# (in my case, registrations_controller.rb, a custom controller)
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages

See original post here

在此处查看原始帖子

...and think about accepting answer, 50% is a bit low! ;)

...想想接受答案,50%有点低!;)

===== EDIT =====

===== 编辑 =====

If you need to redirect to another page when errors occurs, you'll have to override controllers (check Devise Wiki or search stackoverflow for howto) but it should look like something like that

如果您需要在发生错误时重定向到另一个页面,则必须覆盖控制器(检查 Devise Wiki 或搜索 stackoverflow 以获取操作方法)但它应该看起来像这样

# CUSTOM DEVISE CONTROLLER
class RegistrationsController < Devise::RegistrationsController

  # POST /resource
  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?     
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => redirect_location(resource_name, resource)
      else
        set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords(resource)
      # Solution for displaying Devise errors on the homepage found on:
      # https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
      flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
      redirect_to root_path # HERE IS THE PATH YOU WANT TO CHANGE
    end
  end
end

回答by Venkatesh Dhanasekaran

Just add this code under devise->sessions->new.html.erb(View part)
  <%= devise_error_messages! %>
    <% flash.each do |key, value| %>
       <div class="flash <%= key %>"><%= value %></div>
    <% end %>

回答by Naysawn

After a lot of searching, I found that the following worked pretty well for my purposes. Just create the Devise Helper and store the resource.errors.full_messages in a flash error container.

经过大量搜索,我发现以下内容非常适合我的目的。只需创建设计助手并将 resource.errors.full_messages 存储在闪存错误容器中。

Specifically:

具体来说:

module DeviseHelper

  def devise_error_messages!
     flash[:error] = resource.errors.full_messages.first
  end
end

回答by NoDisplayName

just a little thing to add:

只需要补充一点:

Do not print the entire flash hash, print only specific keys. In some circumstances, Devise adds a :timedout key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.

不要打印整个闪存哈希,只打印特定的键。在某些情况下,Devise 会在 flash 哈希中添加一个 :timedout 键,该键不用于显示。如果您打算打印整个散列,请从散列中删除此键。

回答by Sushant

Over riding your devise controllers may help or work when you try to test that on your local but it won't work when you deploy your code. And over riding the gems controller is also a bad practice. So, best way to do it, is to make a new controller, in controllers 'registrations_controller.rb'. and in that controller. " Class RegistrationsControlller < Devise::RegistrationsController " And over ride the methods in that controller.

当您尝试在本地测试它时,过度使用您的设计控制器可能会有所帮助或工作,但在您部署代码时它将不起作用。过度使用宝石控制器也是一种不好的做法。因此,最好的方法是在控制器“registrations_controller.rb”中创建一个新控制器。并在该控制器中。“ Class RegistrationsControlller < Devise::RegistrationsController ”并覆盖该控制器中的方法。