2009-11-13 55 views
6

我想做的事情看似簡單,但可能不是「正確」的添加額外參數到軌航線資源

讓我們說我有一個圖像資源,我操縱基於URL中的圖像。在URL中,我想指定它的大小以及它是灰色,彩色還是灰色或其他條件。

目前我有一些看起來像這樣的命名路線。

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

我的訣竅是,如果我創造了這個期運用Rails的資源,我不知道我怎麼會指定:大小:格式,或它的「顏色類型」。

我想我想添加一個成員路由並指定我的參數,如下所示。

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

還有其他時間,我想增加額外的信息到資源路線,但不知道如何。

任何幫助將不勝感激, 謝謝。

+0

另一個命名的路由我想取而代之的是map.cart '購物車/:序',:控制器=> '訂單',:動作=> '秀' map.resource:購物車,給我'/ cart',我可以在session [:order]中存儲這個數字,但是如果cookies被禁用,那麼這個數字就會中斷。 – ToreyHeinz 2009-11-13 23:45:29

+0

我可以使用條件 「:條件 - 指定自定義路由識別條件。\ Resources設置方法特定路由的方法值。」 – ToreyHeinz 2009-11-13 23:59:14

回答

0

我意識到我想要表示我的資源的方式不在正常的Rails資源範圍之內,沒關係。我真正遇到的問題是,每次都添加了另一個動作和命名路線以達到我想要的感覺,我反覆思考,無論是在我的路線還是在我的行動中。

我回去簡單地創建我的命名路線,並花了一點時間在控制器中,這樣我就可以保持我的路線簡單。下面是我現在擁有的東西,我對它很滿意。

#routes.rb 
map.with_options :controller => 'sketched_images', :action => 'show', :path_prefix => '/sketches', :name_prefix => 'sketched_', :color => 'grey' do |m| 
    m.style "styles/:style/:color/:size.:format" 
    m.design "designs/:design/:color/:size.:format" 
    m.product "products/:product/:color/:size.:format" 
    m.color_combo "colored_products/:color_combo/:size.:format" 
end 

class SketchedImagesController < ApplicationController 
caches_page :show 

before_filter :load_data 
def show 
    @size = params[:size] || 100 
    respond_to do |wants| 
    wants.png 
    wants.jpg 
    end 
end 

private 

def load_data 
    case 
    when params[:design] 
    @image = ClothingDesign.from_param(params[:design]).sketched_image 
    greyed 
    when params[:style] 
    @image = ClothingStyle.from_param(params[:style]).sketched_image 
    greyed 
    when params[:product] 
    @image = Product.from_param(params[:product]).sketched_images.first 
    greyed 
    when params[:color_combo] 
    @color_combo = ColorCombo.find_by_id(params[:color_combo]) 
    @object = @color_combo.colorable 
    if @object.active? && [email protected]_images.blank? 
     @image = @object.sketched_images.first 
     colored 
    else 
     @image = @product.style.sketched_image 
     dimmed 
    end 
    end 
end 

def greyed 
    @blank = "#FFF" 
    @print = "#000" 
    @highlight = "#666" 
end 

def colored 
    @blank = "##{@color_combo.blank_color.value}" 
    @print = "##{@color_combo.design_color.value}" 
    @highlight = "##{@color_combo.highlight_color.value}" unless @color_combo.highlight_color.blank? 
end 

def dimmed 
    @blank = "#BBB" 
    @print = "#000" 
    @highlight = "#444" 
end 

end 
2

查看documentation for Resources。你會發現這一點:

資源方法接受 下列選項自定義 導致路線: *:要求 - 設置自定義路由參數要求;這是正則表達式(必須匹配匹配的路由)或額外參數的散列。例如:

 map.resource :profile, :path_prefix => ':name', :requirements 

=> {:名稱=>/[A-ZA-Z] + /,:額外=> '值'}

將只匹配如果第一部分是字母的,並將通過參數:額外的控制器。**

+0

對,但是我怎樣才能把':size'添加到我的url的末尾,但是在:format選項之前 – ToreyHeinz 2009-11-13 23:52:52

5

有沒有好辦法刪除資源的控制器/ ID部分。你會得到最近通過與像這樣欺騙的ActionController:

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

將產生像www.site.com/images/product/4/gray/1234.html有以下PARAMS哈希路線:

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

格式不會被顯式傳遞但通過通常的respond_to手段,它將在控制器中可用。

接下來,你必須在控制器中使用一些魔術來欺騙軌道做你想做的事情。

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

這實際上更好地工作作爲過濾器,以便:

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 

然而,爲了利用好這些路線,你將不得不所有這些額外的參數傳遞給您的網址進行通話。像這樣:

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

但幫手使這一點很容易。

module ApplicationHelper 
    def easy_gray_url(image, size, product) 
    gray_url(size, :image_id => image.id, :product => product) 
    end 
end 
+0

這有助於思考我真正想要做什麼,以及我得到的是這個.. 。 這不值得。 – ToreyHeinz 2009-11-14 06:22:50

+0

您有兩個其他選擇:編寫您自己的map.resources版本,或編寫生成所需named_routes的方法。 – EmFi 2009-11-14 09:13:28

+0

謝謝,在輸入和時間給你的答案。 – ToreyHeinz 2009-11-15 03:22:52