我可以使用redirect this line at the end of the routes file所有(无效)网址redirecting in the docs。
match '*a' => redirect('/')
我想进行重定向并在routes.rb
中传递提醒消息
类似的东西(这里的语法错误):
match '*a' => redirect('/'), :alert => "aaargh, you don't want to go to #{params[:a]}"
是否可以这样做?
如果我在视图/控制器中执行此操作,我可以使用redirect_to
并在重定向页面上传递Flash消息(请参阅{{3}}中的示例)。创建一个新的控制器方法并在那里重定向会起作用,但它看起来不太优雅......这是推荐的方式吗?
答案 0 :(得分:3)
稍微调整Jun's answer允许我这样做:
match '*a' => redirect { |p, req| req.flash[:error] = "aaargh, you don't want to go to #{p[:a]}"; '/' }
将无效网页重定向到'/'
以及所需(动态)消息。
如果您转到www.your_website/a_bad_place
,则会传递'/'
上的消息:
aaargh,你不想去
a_bad_place
您可以使用以下方法获取整个违规(无效)网址
#{req.env["HTTP_HOST"]}#{req.env["REQUEST_PATH"]}
并显示:
match '*a' => redirect { |p, req| req.flash[:error] = "aaargh, you don't want to go to #{req.env["HTTP_HOST"]}#{req.env["REQUEST_PATH"]}"; '/' }
你会看到:
aaargh,你不想去
www.yourwebsite/a_bad_place
答案 1 :(得分:1)
我能做的最好的事情是:
match 'PATH' => redirect { |p, req| req.flash[:alert] = "MESSAGE"; '/' }