替换devise_error_messages!有闪讯

Replace devise_error_messages! with flash messages

我使用 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 消息以处理数组: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

我收到这个错误:

undefined local variable or method `flash' for Users::RegistrationsController:Class

我仍然不明白所提到的解决方案出了什么问题,但找到了一个我认为更好的解决方案:

第 1 步:

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

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 中定义一个新方法,它为 bootstrap:

配置 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 通知以解析数组:

<% 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" %>

有点干 - 没有应用程序助手 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 %>