能力具有消息(通知,错误,警告,等等)带嵌入式联系是很好的用户相互作用的角度来看。然而,嵌入一个锚标签内闪消息从控制器是肮脏的。

让我们假设一个闪这样的消息是良好的实用性*:

例闪消息通知有一个嵌入的链接http://img.skitch.com/20090826-xbsa4tb3sjq4fig9nmatakthx3.png (借用了 DailyMile.com)

什么样的战术你会之间控制器,并看来采用这样的东西干净?

有帮助吗?

解决方案

只是想我会分享这个,因为找到答案,我一直在寻找其他地方:

作品在轨道3.1

flash[:notice] = "Proceed to #{view_context.link_to('login page', login_path)}".html_safe

其他提示

格伦*吉伦 有一个办法,他打电话 有用的消息在轨.

我修改了他的代码段的要更多一点的惯用(至少对我来说).

控制器罢了闪这样的:

flash[:notice]      = "Your profile was updated. %s"
flash[:notice_item] = ["Edit again?", edit_profile_path(@profile)]

然后你可能会有助手看起来像这样的东西:

def render_flash_messages(*keys)
  messages = keys.collect do |key|
    content_tag(:p, flash_message_with_item(key), :class => "flash #{key}") if flash[key]
  end.join
  content_tag(:div, messages, :id => "flash_messages") unless messages.blank?
end

def flash_message_with_item(key)
  item = flash["#{key}_item".to_sym]
  substitution = item.is_a?(Array) ? link_to(*item) : item
  flash[key] % substitution
end

看起来只是这样的:

<%= render_flash_messages(:error, :notice, :warning) %>

图(通过 flash_message_with_item 助手)是负责创建锚标记,但是管理控制器什么进入快速信息包括一个可选的资源用于进一步的行动。

您可以创建一个辅助方法呈现出基于闪存的消息传回值谐音。

这是类似于的link_to()中的Rails闪光

# In your controller
flash[:error] = render_to_string(:partial => "sessions/login_failed_message")

# In sessions/_login_failed_message.html.erb
Login failed.  If you have forgotten your password, you can #{link_to('reset it', reset_path)}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top