レール/考案 - のlink_toにカスタマイズフラッシュメッセージ(devise.en.yml)
-
25-09-2019 - |
質問
私は工夫が提供する以下のフラッシュMSGをカスタマイズしたいと思います devise.en.ymlファイルでます:
devise:
failure:
unconfirmed: 'You have to confirm your account before continuing.'
new_user_confirmation_pathへのリンクを取得するために、Rubyのコードと
他の言葉で、私は私のフラッシュメッセージが表示さのような何かをしたいです
'You have to confirm your account before continuing. Didn't receive confirmation instructions?'
「確認メールがまだ届きません?」 new_user_confirmation_pathへのリンクがあります。
私工夫がデフォルトでそれを提供しない原因私は、ユーザーのコントローラを編集せずにこれを行うことができますかどうかを知りたいと思います。
あなたの答えをありがとう!
解決
new_user_confirmation_path
は/users/confirmation/new
あなたは自分のdevise.en.ymlファイルでこれを行うことができますので、ます:
devise:
failure:
unconfirmed: "You have to confirm your account before continuing. <a href='/users/confirmation/new'>Didn't receive confirmation instructions?</a>"
あなたはフラッシュメイクを表示するコントローラのアクションで必ずお持ち.html_safe
例えばflash[:error].html_safe
他のヒント
私のアプリケーションの一つで、私は工夫とカンカンを使用しています。私のアプリケーションconrollerに次のようなものとカンカンのエラーからI救助ます。
rescue_from CanCan::AccessDenied do |exception|
if current_user
flash[:error] = exception.message
redirect_to root_url
else
flash[:error] = t("devise.failure.unauthenticated")
redirect_to new_user_session_path
end
end
おそらく、あなたは類似しており、考案から救出何かをするだろうか?あなたのフラッシュメッセージは、その後のようなものである可能性があります:
flash[:error] = t("devise.failure.unconfirmed") + link_to "Didn't receive confirmation instructions?", new_user_confirmation_path
であっても良いだろう置くために「受信しませんでした...」必要であれば、それに翻訳のYMLです。
私が考案フラッシュメッセージに必要なリンクやその他の情報を追加するのRails 3&レール4のための正しい方法は、あなた自身のDevise::FailureApp
を書くことだと思います
# lib/custom_failure.rb
class CustomFailure < Devise::FailureApp
def i18n_options(options)
path = Rails.application.routes.url_helpers.new_user_confirmation_path
link = ActionController::Base.helpers.link_to(I18n.t('.unconfirmed_link_text', options), path)
super(options).merge(new_user_confirmation_link: link)
end
end
と翻訳に補間を追加します:
devise:
failure:
unconfirmed: You have to confirm your account before continuing.%{new_user_confirmation_link}
unconfirmed_link_text: Didn't receive confirmation instructions?
config/initializers/devise.rb
に追加することを忘れないでください。
config.warden do |manager|
manager.failure_app = CustomFailure
end
...あなたは、あなたがこのメッセージを表示したい私たちのビューに配置する必要があります。
例えばdevise/new.erb
について
<%= t('devise.failure.unconfirmed',
:confirm_link => link_to(
t('devise.failure.confirm_link_text'),
new_user_confirmation_path).html_safe
) unless user.confirmed %>
また、あなたのデバイスの特定のレイアウトでそれを行うことができます。app/views/layouts/devise.html.slim
- flash.each do |type, message|
- if ['alert', 'error', 'notice'].include?(type.to_s)
.alert-box class=type
=> message
- case message
- when t('devise.failure.unauthenticated')
= link_to "Forgot your password?", new_password_path(resource_name)
- when t('devise.failure.unconfirmed')
= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name)
- when t('devise.failure.locked')
- if resource_class.unlock_strategy_enabled?(:email)
= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name)