有沒有好辦法刪除資源的控制器/ 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
另一個命名的路由我想取而代之的是map.cart '購物車/:序',:控制器=> '訂單',:動作=> '秀' map.resource:購物車,給我'/ cart',我可以在session [:order]中存儲這個數字,但是如果cookies被禁用,那麼這個數字就會中斷。 – ToreyHeinz 2009-11-13 23:45:29
我可以使用條件 「:條件 - 指定自定義路由識別條件。\ Resources設置方法特定路由的方法值。」 – ToreyHeinz 2009-11-13 23:59:14