Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra params to rails route resources

What I want to do seems simple, but might not be "proper"

let's say I have an image resource, and I manipulate the image based on the url. In the url I want to specify it's size and whether it's grayed, colored, or dimmed or some other condition.

currently I have a number of named routes that look like this.

map.gray_product_image "images/:product/:image/gray/:size.:format", :controller => 'images', :action => 'gray_product_image'

for me the trick is that if I created this useing Rails resources, I don't know how I would specify the :size, :format, or it's "color type".

I guess I would like to add a member route and specify my params like the following.

map.resources :products do |products| 
  products.resources :images, :member => {:gray_product_image => {':image/:size.:format' => :get}}
end

There are other times where I have wanted to added extra info to a resource route but didn't know how.

Any help would be greatly appreciated, Thanks.

like image 244
ToreyHeinz Avatar asked Nov 13 '09 23:11

ToreyHeinz


2 Answers

There's no good way to remove the controller/id part of a resource. The closest you're going to get through tricking ActionController with something like this:

map.resources :gray, :path_prefix => "/images/:product/:image_id/", 
  :controller => 'images', :requirements => {:colour => "gray"}

Which will produce routes like www.site.com/images/product/4/gray/1234.html with the following params hash:

params => { 
  :image_id => 4,
  :id => 1234,
  :colour => "gray",
  :product => "product"
}

The format won't be passed explicitly but it will be available in the controller through the usually respond_to means.

Next you'll have to work some magic in controller to trick rails into doing what you want.

class ImagesController < ApplicationController
  def show
    @size = params[:id]
    @image = Image.find(params[:image_id])
    ...
  end
end

This actually works better as a filter so:

class ImagesController < ApplicationController
  def initialize_colour
    unless params[:colour].nil?
      @size = params[:id]
      @colour = params[:colour]
      @image = Image.find(params[:image_id])
    end
  end

  before_filter :initialize_colour, :except => [:index, :new, :create]

  ...

end

However to make good use of these routes, you're going to have to pass all those extra parameters to your url for calls. Like this:

gray_url(size, :image_id => @image.id, :product => product)

But helpers make that easy.

module ApplicationHelper
  def easy_gray_url(image, size, product)
    gray_url(size, :image_id => image.id, :product => product)
  end
end
like image 89
EmFi Avatar answered Nov 07 '22 09:11

EmFi


Check out the documentation for Resources. You'll find this:

The resources method accepts the following options to customize the resulting routes:

  • :requirements - Set custom routing parameter requirements; this is a hash of either regular expressions (which must match for the route to match) or extra parameters. For example:

    map.resource :profile,
                 :path_prefix => ':name',
                 :requirements => { :name => /[a-zA-Z]+/, :extra => 'value' }
    

    will only match if the first part is alphabetic, and will pass the parameter :extra to the controller.

like image 22
Steve Klabnik Avatar answered Nov 07 '22 11:11

Steve Klabnik