3

我使用生成了我的设计视图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"}

我根据这个答案更改了我的闪存消息以处理数组:rails - 设计 - 处理 - 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

我收到此错误:

Users::RegistrationsController:Class 的未定义局部变量或方法“flash”

4

3 回答 3

6

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

第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定义一个为引导配置您的 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步

确保在您的设计注册视图中调用 NEW devise_error_messages!,它将所有错误添加到闪存消息中,然后可以使用您的闪存渲染布局(在我的情况下_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" %>
于 2015-03-10T13:22:44.487 回答
0

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

添加.alert-error你的 CSS

<% 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 %>
于 2017-12-03T16:12:42.790 回答
0

由于我可以省略 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 %>
于 2018-01-01T18:12:14.240 回答