我使用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
我收到此错误:
对于Users :: RegistrationsController:Class未定义的局部变量或方法`flash'
答案 0 :(得分:5)
我仍然不明白上述解决方案出了什么问题,但我发现它更好一点:
第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" %>
答案 1 :(得分:0)
多一点干 - 没有应用程序助手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 %>
答案 2 :(得分: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 %>