0

我有一个从其父级继承其表和控制器的资源。它的父级也有我想传递给它的自定义路由,但我不确定如何(或者是否可能)。目前的路线如下所示:

resources :publications do
  resources :editions, :controller => :publications
  collection do
    get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
    post :review
  end
  member do
    get :audit, :reassign_prompt
    post :approve, :audit_vote
    put :reassign
  end
end

在当前设置下,版本模型无法访问自定义方法,如“审核”或“自动完成”。是否可以做类似“:routes => :publications”的事情?

4

1 回答 1

1

查看路由问题

# Define the concern
concern :somethingable do
  collection do
    get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
    post :review
  end
  member do
    get :audit, :reassign_prompt
    post :approve, :audit_vote
    put :reassign
  end
end

# And now your routing
resources :publications, concerns: :somethingable do
  resources :editions, controller: :publications, concerns: :somethingable
end

我相信你能想出一个比:somethingable描述常见行为更好的术语

更新

因为以上是针对 railsmaster分支的,所以您可以使用几种替代方法

  1. 有一个gem可以抽象出这种行为,以便在 Rails 3.2+ 中使用
  2. 而不是使用concern,只需在您的路由文件中创建一个方法。

    def somethingable
      collection do
        get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
        post :review
      end
      member do
        get :audit, :reassign_prompt
        post :approve, :audit_vote
        put :reassign
      end
    end
    

    那么你的路线可能看起来像

    resources :publications do
      somethingable
    
      resources :editions, controller: :publications do
        somethingable
      end
    end
    
于 2012-11-27T20:21:31.460 回答