gal*_*gal 12 ruby ruby-on-rails devise ruby-on-rails-3
我正在使用设计,我将登录和注册表单放在同一页面中,现在当我写无效的登录详细信息或不在注册表单中填写所需的输入数据时.
我正在重定向到/users页面,如果我正在尝试注册,或者/users/sign_in如果我尝试使用我所犯的错误登录...
我想留在同一页面并在同一页面中显示错误.
我该怎么做?
非常感谢你,我需要一个快速的帮助:)
Luc*_*cas 18
前段时间我在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 %>
Run Code Online (Sandbox Code Playgroud)
和
# 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
Run Code Online (Sandbox Code Playgroud)
在这里查看原始帖子
......考虑接受答案,50%有点低!;)
=====编辑=====
如果您在发生错误时需要重定向到另一个页面,则必须覆盖控制器(检查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
Run Code Online (Sandbox Code Playgroud)
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 %>
Run Code Online (Sandbox Code Playgroud)
小智 5
经过大量的搜索,我发现以下内容对我来说非常有用.只需创建Devise Helper并将resource.errors.full_messages存储在flash错误容器中.
特别:
module DeviseHelper
def devise_error_messages!
flash[:error] = resource.errors.full_messages.first
end
end
Run Code Online (Sandbox Code Playgroud)