简体   繁体   English

在 Rails 3 路线中更改参数

[英]Changing params in Rails 3 routes

On one of our controllers, we're getting incoming Google traffic to an invalid URL param:在我们的一个控制器上,我们将传入的 Google 流量发送到无效的 URL 参数:

/search/?klass=forum /搜索/?klass=论坛

Instead, it should be:相反,它应该是:

/search/?klass=forums /搜索/?klass=论坛

Currently I have some controller#index code that does this:目前我有一些执行此操作的控制器#index 代码:

    if params[:klass] == "forum"
        params.delete(:action) && params.delete(:controller)
        params[:klass] = "forums"
        @query = params.to_query
        redirect_to "/search?#{@query}", :status => 301 and return
    end

I'm curious if this is possible to do in routes.rb so it doesn't hit our stack but still properly 301's.我很好奇这是否可以在 routes.rb 中做到,所以它不会影响我们的堆栈,但仍然是正确的 301。 I've seen regex's used to validate certain params in the query string, but none used to rewrite the values of those params.我见过正则表达式用于验证查询字符串中的某些参数,但没有一个用于重写这些参数的值。

You can try to handle it with nginx or apache, or you can use this hacky solution using Rack:您可以尝试使用 nginx 或 apache 来处理它,或者您可以使用 Rack 使用这个 hacky 解决方案:

get "/search/" =>  proc { |env| Rack::Request.new(env).params['klass'] == "forum" ? [ 302, {'Location'=> "/search/?klass=forums" }, [] ] : [ 302, {'Location'=> "/" }, [] ] }

Maybe you could declare this route with :constraints => {:klass => 'forum'} and forward it with the correct value for klass .也许你可以用:constraints => {:klass => 'forum'}声明这条路线,并用正确的klass值转发它。 Or why don't you just append the missing 's' in the controller?或者你为什么不只是 append controller 中缺少的“s”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM