是否可以在确认后添加闪存消息的链接?

HUS*_*TEN 6 ruby-on-rails devise ruby-on-rails-3

我正在使用Devise.
当用户成功确认时,将显示flash消息.
我想添加一个链接.

所以我想要这条消息

Your account was successfully confirmed. You are now signed in. Go to your profile page, and edit it!

那部分profile应该是链接example.com/users/username/edit

我怎样才能成功呢?

devise.en.yml

confirmations:
  confirmed: 'Your account was successfully confirmed. You are now signed in.'
Run Code Online (Sandbox Code Playgroud)

dch*_*cke 12

您可以使用该方法view_context访问模型和控制器内的任何视图方法.

例如:

def index
  flash[:notice] = "Go to your #{view_context.link_to("profile page", link_path)}, and edit it!"
  redirect_to link_path
end
Run Code Online (Sandbox Code Playgroud)

相应更新link_path.

  • 当我使用这种方法时,我不得不在消息字符串的末尾添加.html_safe.没有它,锚标记及其内容在页面上显示为纯文本. (5认同)

bry*_*nus 3

我在实施这些解决方案时遇到了困难。我所需要的只是 Flash 中的一个简单的 HTML 链接。下面是我在 Rails 4.1 中实现它的方法。我只需要清理 app/views/shared/_flash.html.erb 中的 Flash 消息:

<% flash.each do |name, msg| %>
  <div class="alert alert-<%= name %>">
    <a class="close" data-dismiss="alert">&#215</a>
    <% if msg.is_a?(String) %>
      <div id="flash_<%= name %>"> <%= sanitize(msg) %> </div>
    <% end %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我只是直接输入 HTML,没有 .html_safe。工作很享受!

flash[:notice] = %Q[Please <a href="#">click here</a>]
Run Code Online (Sandbox Code Playgroud)