Rails routing with query string

I have a problem when I need the values ​​passed from a GET request and I don’t know how to configure the routing definition.

The My Category object has a type (string), color (string), and many products. I want to create a simple web service that allows the caller to get all the products of a category by passing in the type and color of the category:

http://www.myapp.com/getProducts?catType=toy&color=red 

or?

 http://www.myapp.com/categories/getProducts?catType=toy&color=red 

How to determine the correct routing for this situation? Are there any better ways to do this in a Calm manner ... because I know that Rails is Restful, so if there is a way to do it “right” then it will be even better.

thanks

+7
rest query-string ruby-on-rails routing
source share
2 answers

Your first example:

 map.getproduct '/getProduct', :controller => 'your_controller', :action => 'your_action' 

In the controller you will have catType and color in hash parameters:

 params[:catType] => 'toy' params[:color] => 'red' 

Is there a better way? Probably yes, but it depends on your needs. If you always have catType and color options, than you can add a route as follows:

 map.getproduct '/getProduct/:catType/:color', :controller => 'your_controller', :action => 'your_action' 

You will have access to these parameters with the params hash, as in the previous example. And your URLs will look like this:

 www.myapp.com/getProduct/toy/red 

If your settings can change, you can use routing:

  map.getproduct '/getProduct/*query', :controller => 'your_controller', :action => 'your_action' 

He will then catch the entire request, having www.my.app.com/getProduct/... at the beginning. But you will have more work in the controller. You will have access to query with this:

  params[:query] 

and for www.myapp.com/getProduct/color/red/catType/toy it will give:

  params[:query] => ['color', 'red', 'catType', 'toy] 

So you have to disassemble it manually.

+11
source share

One RESTful way to do this would be to include a product resource nested under a category resource, for example:

http://www.myapp.com/categories/toy/products?color=red

Your .rb routes should contain:

  map.resources :categories do |category| category.resources :products end 

Since my url is above, using an attribute of type Category for routing, I mean that each type is unique, like id. This means that whenever you load a category into the Category controller (or elsewhere), you need to load the Category Category.find_by_type(params[:id]) category instead of Category.find(params[:id]) . I like routing categories whenever possible.

Your ControlController product pointers will find products using strings such as:

  @category = Category.find_by_type(params[:category_id]) @products = @category.products.find(:all, :conditions => { :color => params[:color]} ) 

Remember that your category model must contain the line:

 has_many :products 

It is probably a good idea to enforce in a model with checks:

 validates_presence_of :type validates_uniqueness_of :type 

To do the routing work, you must also rewrite the to_param method in the Category model to return a type instead of id:

 def to_param self.type end 
+4
source share

All Articles