Rails 3 路由:包含带根重定向的 flash 参数
在 Rails 3 中,您可以设置根路径以直接从 config/routes.rb 重定向到其他地方:
root :to => redirect("/dashboard")
这可行,只是它会清除传递给 root 的任何 flash 参数。如何通过重定向传递它们?
更新: James Chen的解决方案,即将路由声明重写为 root :to = >重定向 { |p, 请求|请求.flash.keep; “/dashboard”}
,对我有用。但有两件事我不明白:
p
代表什么,params?我尝试使用
do
/end
和换行符重写该块:root :to =>;重定向做 |p, req| 请求闪存保持 “/仪表板” 结尾
但这会失败,并显示“ArgumentError:不支持重定向参数”。这是为什么?
In Rails 3 you can set up your root path to redirect elsewhere right from config/routes.rb:
root :to => redirect("/dashboard")
This works, except that it wipes out any flash params passed to root. How can you pass them along through the redirect?
Update: James Chen's solution, i.e. rewriting the route declaration as root :to => redirect { |p, req| req.flash.keep; "/dashboard" }
, works for me. But there are two things I don't understand about it:
- What does
p
stand for, params? I tried rewriting the block with
do
/end
and linebreaks:root :to => redirect do |p, req| req.flash.keep "/dashboard" end
But this fails with "ArgumentError: redirection argument not supported". Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
flash.keep
。在路由文件中编写重定向过程:
因此,将传递从正常重定向到根 URL 的 flash 参数:
Use
flash.keep
.Write a proc for the redirect in your routes file:
So flash params from normal redirect to root url will be passed:
为什么你不做一些类似
假设“/dashboard”导致仪表板控制器索引操作的事情。
更新
我的意思是,如果您真的想要,您可以添加到“/dashboard”处显示的 html.erb 页面
,然后再次执行类似的操作
...叹息..
忘记了,您可以根<代码>:到=> '无论你原来的#rootareawas'
然后就这样做:
在你的任何原始控制器根区域中执行操作
这确实很脏,但现在是凌晨 2 点,我必须从伊利诺伊州开车到佐治亚州。
希望这有帮助
why wouldnt you just do something like
assuming that '/dashboard' leads to the dashboard controller index action.
Update
I mean if you reallllly wanted to you could add to the html.erb page that is displayed at '/dashboard'
and do something like this
Edit again... sigh..
Forgot, you could just root
:to => 'whateveryouroriginal#rootareawas'
then just do:
in your whateveryouroriginal controller rootareawas action
This is really dirty but its 2am and i have to drive to Georgia from Illinois.
Hope this helps