tôi tìm thấy giải pháp cho vấn đề này trên Stackoverflow một số thời gian trước đây. Đây là những gì làm việc cho tôi
# 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 %>
và
# 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
Xem bài gốc here
... và nghĩ về việc chấp nhận câu trả lời, 50% là một chút thấp! ;)
===== EDIT =====
Nếu bạn cần phải chuyển hướng đến một trang khác khi lỗi xảy ra, bạn sẽ phải ghi đè lên bộ điều khiển (kiểm tra lập mưu Wiki hoặc tìm kiếm stackoverflow cho howto) nhưng nó sẽ trông giống như một cái gì đó như thế
# 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
không làm việc ... khi tôi bấm vào đăng nhập hoặc đăng ký tôi stil chuyển hướng đến trang anthoer và có tôi nhận được các lỗi ... – gal
Bạn cần phải ghi đè lên bộ điều khiển và thay đổi của bạn tuyến đường phản hồi. Xem chỉnh sửa của tôi. – Lucas
bạn quên yêu cầu tôi đưa vào các tuyến đường:: registrations => "registration" – gal