entity
entity

Reputation: 41

Changing params in Rails 3 routes

On one of our controllers, we're getting incoming Google traffic to an invalid URL param:

/search/?klass=forum

Instead, it should be:

/search/?klass=forums

Currently I have some controller#index code that does this:

    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. I've seen regex's used to validate certain params in the query string, but none used to rewrite the values of those params.

Upvotes: 2

Views: 481

Answers (2)

RocketR
RocketR

Reputation: 3766

Maybe you could declare this route with :constraints => {:klass => 'forum'} and forward it with the correct value for klass. Or why don't you just append the missing 's' in the controller?

Upvotes: 0

fl00r
fl00r

Reputation: 83680

You can try to handle it with nginx or apache, or you can use this hacky solution using Rack:

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

Upvotes: 1

Related Questions