在 Rails 中可以同时使用嵌套资源和浅层资源吗?如何编写控制器/视图?

发布于 2024-10-19 03:50:24 字数 912 浏览 2 评论 0原文

我拥有一些资源,将它们与其他资源嵌套并单独处理是非常有意义的。即我希望使用像这样的所有网址:

/account/4/transfers   # all transfers which belong to an account
/user/2/transfers      # all transfers input by specific user
/project/1/transfers   # all transfers relevant to a project
/transfers             # all transfers

我关心的是如何编写 TransfersController 操作(例如索引),因为它会使父模型中找到的逻辑加倍 - 有没有比做类似的事情更好的方法

TransfersController
...
def index
  if !params[account_id].nil?
    @account = Account.find(params[account_id])
    @transfers = @account.transfers
  elsif !params[user_id].nil?
    @user = User.find(params[user_id])
    if @user.accesible_by?(current_user)
      @transfers = @user.transfers
    end
  elsif !params[projects_id].nil?
    .....

,同样适用于视图- 虽然它们都会列出传输,但它们对于用户、帐户、项目等会有非常不同的标题、导航等......

我希望您看到这个示例中的模式。我认为应该有一些不丑陋的解决方案来解决这个问题。基本上,我希望将选择要显示的传输的逻辑与其他内容(例如视图的上下文特定部分)分开。

I have resources for which it makes perfect sense to address them both as nested withing other resources and separately. I.e. i expect to use all urls like these:

/account/4/transfers   # all transfers which belong to an account
/user/2/transfers      # all transfers input by specific user
/project/1/transfers   # all transfers relevant to a project
/transfers             # all transfers

my concern is how do I write TransfersController actions (for example index) as it would double the logic found in parent models - is there a better way than doing something like

TransfersController
...
def index
  if !params[account_id].nil?
    @account = Account.find(params[account_id])
    @transfers = @account.transfers
  elsif !params[user_id].nil?
    @user = User.find(params[user_id])
    if @user.accesible_by?(current_user)
      @transfers = @user.transfers
    end
  elsif !params[projects_id].nil?
    .....

and the same holds for views - although they all will list transfers they will have very different headers, navigation etc for user, account, project, ...

I hope that you see the pattern from this example. I think there should be some non-ugly solution to this. Basically I would love to separate the logic which selects the transfers to be displayed and other things like context specific parts of view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-10-26 03:50:24

我对此有一个悬而未决的问题。在我的问题中,我概述了我想到的两种方法。我目前正在使用第二个,效果很好。

在 Rails 3 中路由嵌套资源

我使用的路线有点不同因为我使用用户名代替 ID,并且我首先需要它们。你会坚持这样的说法:

namespace :projects, :path => 'projects/:project_id' do
  resources :transfers #=> controllers/projects/transfers_controller.rb
end

# app/controllers/projects/transfers_controller.rb
module Projects
  class TransfersController < ApplicationController
    # actions that expect a :project_id param
  end
end

# app/controllers/transfers_controller.rb
class TransfersController < ApplicationController
  # your typical actions without any project handling
end

我使用命名空间而不是调用资源的原因是让 Rails 让我使用具有单独视图的单独控制器来处理相同的模型,而不是推送所有令人讨厌的条件逻辑进入我的控制器操作。

I've got an open question on this. In my question I outline the 2 methods I came up with. I'm using the second currently, and it's working pretty well.

Routing nested resources in Rails 3

The route I'm using is a bit different because I'm using usernames in place of the IDs, and I want them first. You would stick with something like:

namespace :projects, :path => 'projects/:project_id' do
  resources :transfers #=> controllers/projects/transfers_controller.rb
end

# app/controllers/projects/transfers_controller.rb
module Projects
  class TransfersController < ApplicationController
    # actions that expect a :project_id param
  end
end

# app/controllers/transfers_controller.rb
class TransfersController < ApplicationController
  # your typical actions without any project handling
end

The reason I use the namespace instead of a call to resources is to have Rails let me use a separate controller with separate views to handle the same model, rather than pushing all the nasty conditional logic into my controller actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文