2011-11-07 15 views
0

我有這樣的結構,我的新網址:Rails的從舊的URL到一個新的重定向,使用塞

module ApplicationHelper 
    def product_path(product) 
    unless product.category.nil? 
     cat = product.category 
     if cat.ancestry_depth.eql?(2) 
     product_long_path cat.root, cat.parent, cat, product 
     else 
     product_short_path cat.parent, cat, product 
     end 
    end 
    end 
end 

的routes.rb文件

get "/(*root_id)_(*subcategory_id)_(*category_id)_(*id)" => "products#show", :as => :product_long 
    get "/(*root_id)_(*category_id)_(*id)" => "products#show", :as => :product_short 

和我的表演動作

def show 
    @product = Product.find_by_slug params[:id] 
    cat = @product.category 

    raise NotFound unless cat.slug.eql?(params[:category_id]) and cat.root.slug.eql?(params[:root_id]) 
    raise NotFound if cat.ancestry_depth.eql?(2) and !cat.parent.slug.eql?(params[:subcategory_id]) 
    end 

我如何建立一個"products/:id"重定向,我有2和3類深度的產品。

回答

1

我做的最明顯的方式:
的ProductsController,展現在產品型號

def redirect_string 
    if category.ancestry_depth.eql?(2) 
     "/#{category.root.slug}_#{category.parent.slug}_#{category.slug}_#{slug}" 
    else 
     "/#{category.parent.slug}_#{category.slug}_#{slug}" 
    end 
    end 

但林不知道,這是最優雅的解決方案行動

@product = Product.find_by_slug params[:id] 
if @product.nil? 
    @product = Product.find params[:id] 
    redirect_to @product.redirect_string and return 
end 

,所以IM打開另一個解決方案。

相關問題