如何在 Rails 3 中的 redirect_to 调用中允许自定义闪存键 [英] How to allow custom flash keys in a redirect_to call in Rails 3

查看:49
本文介绍了如何在 Rails 3 中的 redirect_to 调用中允许自定义闪存键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 3 中,您可以将 has 属性直接传递给 redirect_to 以设置 flash.例如:

In Rails 3, you can pass has attributes directly to redirect_to to set the flash. For example:

redirect_to root_path, :notice => "Something was successful!"

然而,这只适用于 :alert:notice 键;如果要使用自定义键,则必须使用更详细的版本:

However, this only works with the :alert and :notice keys; if you want to use custom keys, you have to use a more verbose version:

redirect_to root_path, :flash => { :error => "Something was successful!" }

有没有什么办法可以让自定义键(例如上面的:error)可以传递给redirect_to 而无需在:flash 中指定它=>{}?

Is there any way to make it so that custom keys (such as :error, above) can be passed to redirect_to without specifying it in :flash => {}?

推荐答案

在 Rails 4 中你可以这样做

In Rails 4 you can do this

class ApplicationController < ActionController::Base
  add_flash_types :error, ...

然后在某个地方

redirect_to root_path, error: 'Some error'

http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013

这篇关于如何在 Rails 3 中的 redirect_to 调用中允许自定义闪存键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆