在Ruby中使用flash传递消息

来源:互联网 发布:貂皮能在淘宝上买吗 编辑:程序博客网 时间:2024/05/06 07:22
Ruby中可以通过flash[:error]或者flash[:notice]从controller中传递给页面错误和提示信息。但是在传递的时候需要注意,在这个需要传递消息的action中一定要使用redirect_to把页面跳转,如果不进行调转,则会在用户点击下一个连接的时候还会出现这个error或者notice的消息。

在rails的文档里面写道:

The flash provides a way to pass temporary objects between actions.Anything you place in the flash will be exposed to the very next action andthen cleared out.

另外,flash的原理相当于是在一个hash里面放置数据,所以不用仅仅局限于:error和:notice,我们可以把消息更加细节化,比如说flash[:login_error],这样就可以在页面上的指定位置显示登陆错误了。
原创粉丝点击