4

因此,我刚刚与一位同事进行了激烈的宗教争论。

我们有对象模型和 routes.rb:

resources :orgs do
  resources :lists do
    resources :posts do
      resources :replies
    end
  end
end

所有这些关系都是一对多的,即一个列表将始终恰好属于 1 个组织,一个帖子将始终恰好属于 1 个列表等。我们非常清楚对多嵌套路由的普遍厌恶,但有意识地决定朝这个方向走。

不幸的是,这意味着当您想要链接到编辑回复时,您必须编写:

edit_org_list_post_reply_path( reply.post.list.org, reply.post.list, reply.list, reply )

感觉很傻。

我希望能够做类似的事情:

fast_path( action: :edit, model: reply, type: :path )

并利用回复仅属于一个帖子、属于一个列表等这一事实来计算其余部分。就像是:

def fast_path options
  action = options[:action]
  model = options[:model]
  path_or_url = options[:type]

  model_fields = {
    reply: [:org, :list, :post]
    post: [:org, :list],
    list: [:org]
  }[model.class.name]

  arguments = model_fields.map { |field| model.send(field) } + [model]
  named_route_name = model_fields.join("_") + "_" + path_or_url
  named_route_name = action + "_" + named_route_name if action

  send(named_route_name, arguments)
end

虽然我还没有验证这是否有效或者是特别好的代码。

但是,我的同事以前做过类似的事情,他重写了许多命名路线,以使它们更易于调用。他声称这只会导致痛苦、绝望和折磨,并且这次他正在竭尽全力将这些尝试排除在我们的代码之外。

我很高兴错了,所以请告诉我们您的想法!

4

2 回答 2

1

如果您不想覆盖路径助手(无疑有充分的理由不这样做),您可以向Reply模型添加一个方法,如下所示:

def path_args  
  [post.list.org, post.list, post, self]
end

然后只需调用:edit_org_list_post_reply_path(*reply.path_args)

于 2013-01-03T23:14:51.743 回答
1

你考虑过浅嵌套吗?

resources :orgs, shallow: true do
  resources :lists, shallow: true do
    resources :posts, shallow: true do
      resources :replies
    end
  end
end

然后你仍然会得到所有集合的深路径,但所有成员的浅路径:

+---------------------+-----------+---------------------------------------+-------------------+
| Helper              | HTTP Verb | Path                                  | Controller#Action |
+---------------------+-----------+---------------------------------------+-------------------+
| post_replies_path   | GET       | /posts/:post_id/replies(.:format)     | replies#index     |
|                     | POST      | /posts/:post_id/replies(.:format)     | replies#create    |
| new_post_reply_path | GET       | /posts/:post_id/replies/new(.:format) | replies#new       |
| edit_reply_path     | GET       | /replies/:id/edit(.:format)           | replies#edit      |
| reply_path          | GET       | /replies/:id(.:format)                | replies#show      |
|                     | PATCH     | /replies/:id(.:format)                | replies#update    |
|                     | PUT       | /replies/:id(.:format)                | replies#update    |
|                     | DELETE    | /replies/:id(.:format)                | replies#destroy   |
| list_posts_path     | GET       | /lists/:list_id/posts(.:format)       | posts#index       |
|                     | POST      | /lists/:list_id/posts(.:format)       | posts#create      |
| new_list_post_path  | GET       | /lists/:list_id/posts/new(.:format)   | posts#new         |
| edit_post_path      | GET       | /posts/:id/edit(.:format)             | posts#edit        |
| post_path           | GET       | /posts/:id(.:format)                  | posts#show        |
|                     | PATCH     | /posts/:id(.:format)                  | posts#update      |
|                     | PUT       | /posts/:id(.:format)                  | posts#update      |
|                     | DELETE    | /posts/:id(.:format)                  | posts#destroy     |
| org_lists_path      | GET       | /orgs/:org_id/lists(.:format)         | lists#index       |
|                     | POST      | /orgs/:org_id/lists(.:format)         | lists#create      |
| new_org_list_path   | GET       | /orgs/:org_id/lists/new(.:format)     | lists#new         |
| edit_list_path      | GET       | /lists/:id/edit(.:format)             | lists#edit        |
| list_path           | GET       | /lists/:id(.:format)                  | lists#show        |
|                     | PATCH     | /lists/:id(.:format)                  | lists#update      |
|                     | PUT       | /lists/:id(.:format)                  | lists#update      |
|                     | DELETE    | /lists/:id(.:format)                  | lists#destroy     |
| orgs_path           | GET       | /orgs(.:format)                       | orgs#index        |
|                     | POST      | /orgs(.:format)                       | orgs#create       |
| new_org_path        | GET       | /orgs/new(.:format)                   | orgs#new          |
| edit_org_path       | GET       | /orgs/:id/edit(.:format)              | orgs#edit         |
| org_path            | GET       | /orgs/:id(.:format)                   | orgs#show         |
|                     | PATCH     | /orgs/:id(.:format)                   | orgs#update       |
|                     | PUT       | /orgs/:id(.:format)                   | orgs#update       |
|                     | DELETE    | /orgs/:id(.:format)                   | orgs#destroy      |
+---------------------+-----------+---------------------------------------+-------------------+
于 2013-12-12T03:06:38.547 回答