The ability to have flash messages (notice, error, warning, etc) with embedded links is nice from a user interaction standpoint. However, embedding an anchor tag inside a flash message from the controller is dirty.
从用户交互的角度来看,具有嵌入式链接的闪存消息(通知,错误,警告等)的能力是很好的。但是,在来自控制器的flash消息中嵌入锚标记很脏。
Let's assume that a flash message like this is good for usability*:
让我们假设这样的flash消息对可用性有好处*:
Example Flash Message Notice with an Embedded Link http://img.skitch.com/20090826-xbsa4tb3sjq4fig9nmatakthx3.png (borrowed from DailyMile.com)
嵌入式链接示例Flash消息通知http://img.skitch.com/20090826-xbsa4tb3sjq4fig9nmatakthx3.png(从DailyMile.com借来)
What tactic would you take between the controller and view to employ something like this cleanly?
在控制器和视图之间采取什么策略来干净利用这样的东西?
4 个解决方案
#1
24
Just thought I would share this, since found answer I was looking for elsewhere:
只是想我会分享这个,因为找到的答案我在其他地方寻找:
Works on rails 3.1
适用于轨道3.1
flash[:notice] = "Proceed to #{view_context.link_to('login page', login_path)}".html_safe
#2
8
Glenn Gillen has an approach that he calls Useful Flash Messages in Rails.
Glenn Gillen有一种方法,他称之为Rails中的有用Flash消息。
I modified his code snippets to be a little more idiomatic (for me at least).
我将他的代码片段修改为更加惯用(至少对我来说)。
The controller fills the flash like this:
控制器像这样填充闪光灯:
flash[:notice] = "Your profile was updated. %s"
flash[:notice_item] = ["Edit again?", edit_profile_path(@profile)]
You then might have helpers that look something like this:
然后你可能会有这样的帮助:
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
The view looks simply like this:
视图看起来像这样:
<%= render_flash_messages(:error, :notice, :warning) %>
The view (via the flash_message_with_item
helper) is responsible for creating the anchor tag, but the controller manages what goes into the flash message including an optional resource for further action.
视图(通过flash_message_with_item帮助程序)负责创建锚标记,但控制器管理进入flash消息的内容,包括用于进一步操作的可选资源。
#3
1
You could create a helper method to render out partials based on the value passed back in the flash message.
您可以创建一个辅助方法,根据flash消息中传回的值渲染部分内容。
#4
1
This is similar to link_to() in Rails flash
这类似于Rails flash中的link_to()
# 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)}
#1
24
Just thought I would share this, since found answer I was looking for elsewhere:
只是想我会分享这个,因为找到的答案我在其他地方寻找:
Works on rails 3.1
适用于轨道3.1
flash[:notice] = "Proceed to #{view_context.link_to('login page', login_path)}".html_safe
#2
8
Glenn Gillen has an approach that he calls Useful Flash Messages in Rails.
Glenn Gillen有一种方法,他称之为Rails中的有用Flash消息。
I modified his code snippets to be a little more idiomatic (for me at least).
我将他的代码片段修改为更加惯用(至少对我来说)。
The controller fills the flash like this:
控制器像这样填充闪光灯:
flash[:notice] = "Your profile was updated. %s"
flash[:notice_item] = ["Edit again?", edit_profile_path(@profile)]
You then might have helpers that look something like this:
然后你可能会有这样的帮助:
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
The view looks simply like this:
视图看起来像这样:
<%= render_flash_messages(:error, :notice, :warning) %>
The view (via the flash_message_with_item
helper) is responsible for creating the anchor tag, but the controller manages what goes into the flash message including an optional resource for further action.
视图(通过flash_message_with_item帮助程序)负责创建锚标记,但控制器管理进入flash消息的内容,包括用于进一步操作的可选资源。
#3
1
You could create a helper method to render out partials based on the value passed back in the flash message.
您可以创建一个辅助方法,根据flash消息中传回的值渲染部分内容。
#4
1
This is similar to link_to() in Rails flash
这类似于Rails flash中的link_to()
# 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)}