3

i wonder if you can route a request to a specific controller with query string.

eg. if a request contains a querystring "q=hello" then it would be sent to a specific controller.

and if it contains "search=hello" then it would be sent to another controller.

is this possible?

thanks!

1
  • Why exactly do you want to do this? Chances are this is an indicator that your controller/model structure needs refactoring. Commented Jul 22, 2010 at 2:10

2 Answers 2

1

That's kind of a strange way to do things, but here's something that might help.

Suppose you have a route like so:

map.connect ':controller/:action/:id'

Then /people/edit/1?gender=male would route to the edit action on the people controller, and params[:id] would be 1 and params[:gender] would be "male".

So to answer your specific question, as far as I know, no, you can't do that. But to do what you're saying wouldn't be very RESTful, if you care about that, and any functionality you need could be done more easily with other routes.

For example, you could have a q and a search action in your controller. With the route above, your examples would be /people/q/hello and /people/search/hello.

Or you could have a route like this:

map.connect ':controller/:action'

And then /people/index?q=hello and /people/index?search=hello could be filtered in the code by an if statement for params[].

To reiterate: No (as far as I know) but you shouldn't want to do that anyway.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution just in case someone reachs here while searching:

Setting up Rails routes based on QueryString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.