[英]Devise Sign-up Errors
我在我的Rails webapp的主頁上有我的注冊表單,我想知道如何在該頁面上顯示注冊錯誤,而不是在/users/sign_up
頁面上。 我試着移動devise_error_message!
阻止進入我的主頁,但在注冊失敗后用戶被重定向到主頁時沒有任何內容。
由於devise_error_messages
在/users/sign_up
頁面上呈現錯誤 - 為什么當我在那里調用它們時它們不顯示在主頁上?
可能是設計輔助方法所期望的“資源”屬性無法解析為任何值。 此外,您不應重定向用戶,只是渲染主頁有錯誤。 我不確定渲染時模型是如何傳遞的。
您可以在主頁幫助程序類中擁有custom_devise_error_messages:
def custom_devise_error_messages!
return "" if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource.class.model_name.human.downcase)
html = <<-HTML
<div class="alert alert-error">
<a class="close" data-dismiss="alert">x</a>
<ul style="padding:0; margin: 0; padding-left: 8px;">#{messages}</ul>
</div>
HTML
html.html_safe
end
此外,從您的注冊操作方法,而不是redirect_to :path_or_url
使用render :action_name
redirect_to :path_or_url
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.