4

ですから、私は同僚との激しい宗教的議論から抜け出しました。

オブジェクトモデルとroutes.rbがあります。

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

これらの関係はすべて1対多です。つまり、リストは常に正確に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 )

返信は1つの投稿にのみ属し、1つのリストに属するなど、残りの部分を解決するために使用します。何かのようなもの:

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 に答える