Rails 3 路由:包含带根重定向的 flash 参数

发布于 2025-01-06 20:40:09 字数 615 浏览 1 评论 0原文

在 Rails 3 中,您可以设置根路径以直接从 config/routes.rb 重定向到其他地方:

root :to => redirect("/dashboard")

这可行,只是它会清除传递给 root 的任何 flash 参数。如何通过重定向传递它们?

更新: James Chen的解决方案,即将路由声明重写为 root :to = >重定向 { |p, 请求|请求.flash.keep; “/dashboard”},对我有用。但有两件事我不明白:

  1. p 代表什么,params?
  2. 我尝试使用 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:

  1. What does p stand for, params?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2025-01-13 20:40:09

使用flash.keep

在路由文件中编写重定向过程:

root :to => redirect { |p, req| req.flash.keep; "/dashboard" }

因此,将传递从正常重定向到根 URL 的 flash 参数:

redirect_to root_url, :notice => "test flash notice"

Use flash.keep.

Write a proc for the redirect in your routes file:

root :to => redirect { |p, req| req.flash.keep; "/dashboard" }

So flash params from normal redirect to root url will be passed:

redirect_to root_url, :notice => "test flash notice"
动次打次papapa 2025-01-13 20:40:09

为什么你不做一些类似

root :to => 'dashboard#index'

假设“/dashboard”导致仪表板控制器索引操作的事情。

更新

我的意思是,如果您真的想要,您可以添加到“/dashboard”处显示的 html.erb 页面

,然后再次执行类似的操作

<% if request.referer.scan('yoursitename').size == 0 %>
 <%= create your own notice here %>
<% end %>

...叹息..

忘记了,您可以根<代码>:到=> '无论你原来的#rootareawas'
然后就这样做:

redirect_to '/dashboard'

在你的任何原始控制器根区域中执行操作

这确实很脏,但现在是凌晨 2 点,我必须从伊利诺伊州开车到佐治亚州。

希望这有帮助

why wouldnt you just do something like

root :to => 'dashboard#index'

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

<% if request.referer.scan('yoursitename').size == 0 %>
 <%= create your own notice here %>
<% end %>

Edit again... sigh..

Forgot, you could just root :to => 'whateveryouroriginal#rootareawas'
then just do:

redirect_to '/dashboard'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文