替换devise_error_messages!用flash消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换devise_error_messages!用flash消息相关的知识,希望对你有一定的参考价值。

我使用rails g devise:views users生成了我的设计视图

我使用rails g devise:controllers users生成了我的设计控制器

我在routes.rb中添加了我的新控制器:

  devise_for :users, controllers: { sessions: "users/sessions", 
                                    confirmations: "users/confirmations",
                                    registrations: "users/registrations", 
                                    passwords: "users/passwords",
                                    unlocks: "users/unlocks"}

我根据这个答案更改了我的flash消息来处理Arrays:rails - Devise - Handling - devise_error_messages

像这样:

<% flash.each do |key, value| %>
    <% if value.class == Array %>
        <% value.each do |message| %>
            <div class="<%= flash_class(key) %>">
                <%= message %>
            </div>
        <% end %>
    <% else %>
        <div class="<%= flash_class(key) %>">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <%= value %>
        </div>

    <% end %>

<% end %>

我的新自定义注册控制器:

class Users::RegistrationsController < Devise::RegistrationsController
# before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # You can put the params you want to permit in the empty array.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.for(:sign_up) << :attribute
  # end

  # You can put the params you want to permit in the empty array.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.for(:account_update) << :attribute
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
  flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
end

当我添加这一行:

flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages

我收到此错误:

用户:: RegistrationsController:类的未定义局部变量或方法`flash'

答案

我仍然不明白上述解决方案出了什么问题,但我发现它更好一点:

步骤1:

创建一个文件devise_helper.rb,您可以在其中覆盖devise_error_messages!方法并将所有错误复制到闪存通知数组

module DeviseHelper
  def devise_error_messages!
    if resource.errors.full_messages.any?
        flash.now[:error] = resource.errors.full_messages
    end
    return ''
  end
end

第2步:

application_helper.rb中定义了一个新方法,用于配置引导程序的html类:

def flash_class(level)
    case level
        when 'notice' then "alert alert-dismissable alert-info"
        when 'success' then "alert alert-dismissable alert-success"
        when 'error' then "alert alert-dismissable alert-danger"
        when 'alert' then "alert alert-dismissable alert-danger"
    end
end

第3步:

配置闪存通知以解析数组:

<% flash.each do |key, value| %>
    <% if value.class == Array %>
        <div class="<%= flash_class(key) %>">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <% value.each do |message| %>  
                <%= message %>
                </br>
            <% end %>
        </div>
    <% else %>
        <div class="<%= flash_class(key) %>">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <%= value %>
        </div>
    <% end %>
<% end %>

第4步

确保在你的设计注册视图中调用新的devise_error_messages!,这会将所有错误添加到flash消息中,然后可以使用flash渲染布局显示(在我的情况下为_error.html.erb):

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>
  <%= render 'layouts/error' %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "users/shared/links" %>
另一答案

多一点DRY - 没有应用程序助手flash_card和重复html的视图:

在CSS中添加.alert-error

<% flash.each do |key, value| %>
    <div class="alert alert-dismissable alert-<% key %>">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <% if value.class == Array %>
            <% value.each do |message| %>  
                <%= message %>
                </br>
            <% end %>
        <% else %>
            <%= value %>
        <% end %>
    </div>
<% end %>
另一答案

由于我可以省略IF是否是数组,我喜欢写如下。

<% flash.each do |key, value| %>
  <div class="alert alert-dismissable alert-<% key %>">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <% [*value].each do |message| %>  
      <%= message %></br>
    <% end %>
  </div>
<% end %>

以上是关于替换devise_error_messages!用flash消息的主要内容,如果未能解决你的问题,请参考以下文章

Chrome 自动填充不正确的数据类型

用正则表达式实现替换

dreamweaver怎么用通配符替换

wps怎么用替换功能

notepad+++查找和替换 不能用

怎样用WPS替换\后面的英文,不能用任意字母,因为前面有要保留的字母。