2011-11-04 28 views
0

有没有什么办法干掉这些路线。有一种模式给他们:如何干掉这些路线

get "articles/new" => "articles#new", :as => :new_article 
post "articles/new" => "articles#create", :as => :create_article 
get "articles/:slug/edit" => "articles#edit", :as => :edit_article 

get "stores/:id/articles/new" => "articles#new", :as => :new_store_article, :defaults => { :scope => 'store' } 
post "stores/:id/articles/new" => "articles#create", :as => :create_store_article, :defaults => { :scope => 'store' } 
get "stores/:id/articles/:slug/edit" => "articles#edit", :as => :edit_store_article, :defaults => { :scope => 'store' } 

get "warehouses/:id/articles/new" => "articles#new", :as => :new_warehouse_article, :defaults => { :scope => 'warehouse' } 
post "warehouses/:id/articles/new" => "articles#create", :as => :create_warehouse_article, :defaults => { :scope => 'warehouse' } 
get "warehouses/:id/articles/:slug/edit" => "articles#edit", :as => :edit_warehouse_article, :defaults => { :scope => 'warehouse' } 

在此先感谢!

回答

0

我想要一个完美的解决方案,我似乎找到了一个。基本上,补充说,我可以把这个在lib/routes_helper.rb在我的路线文件中使用一个辅助方法:

class ActionDispatch::Routing::Mapper 
    def article_resources_for(scope = nil) 
    scope_path_symbol = scope_path = nil 
    defaults = {} 

    unless scope.blank? 
     scope_path = "#{scope}/:id/" 
     scope_path_symbol = "#{scope}_" 
     defaults = { :defaults => { :scope => scope } } 

    get "#{scope_path}articles/new" => "articles#new", { :as => :"new_#{scope_path_symbol}article" }.merge(defaults) 
    post "#{scope_path}articles/new" => "articles#create", { :as => :"create_#{scope_path_symbol}article" }.merge(defaults) 
    get "#{scope_path}articles/:slug/edit" => "articles#edit", { :as => :"edit_#{scope_path_symbol}article" }.merge(defaults) 

    end 
end 

然后在我的routes.rb文件,我可以简单地只是做

article_resources_for 
article_resources_for "stores" 
article_resources_for "warehouses" 
1

您的文章中的slu different是否与article_id不同?尝试添加下面到您的文章模型:

#This overrides the :id in your routes, and uses the slug instead 
def to_param 
    slug 
end 

然后,下面应该在您的路线工作。

resources :articles, :only => [:new, :create, :edit] 
scope :stores do 
    resources :articles, :only => [:new, :create, :edit] 
end 
scope :warehouses 
    resources :articles, :only => [:new, :create, :edit] 
end 

我强烈建议你阅读过http://guides.rubyonrails.org/routing.html

+0

如果我不想要使用资源(我计划添加许多不同于REST的行为)?我将如何解决默认参数问题':defaults => {:scope =>'...'}' – axsuul