Rails 3.1-如何为同一模型组织多个索引操作? [英] Rails 3.1 - How do I organize multiple index actions for the same model?

查看:46
本文介绍了Rails 3.1-如何为同一模型组织多个索引操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个has_many项目的User模型。项目模型属于用户模型。我目前正在使用projects_controller.rb索引操作来显示已在所有用​​户(Project.all)中创建的所有项目。

I have a User model that has_many projects. The Project model belongs_to the User model. I am currently using the projects_controller.rb index action to display all of the projects that have been created across all users (Project.all).

在另一个页面上,我还希望有一种方法可以显示属于特定用户的所有项目(即转到页面并能够查看所有属于给定用户的项目。)

On a separate page, I would also like a way to display all of the projects that belong to a specific user (i.e. go to page and be able to see all of the projects that belong to a given user).

我很难确定要使用哪个控制器/操作/视图以及如何设置路线,因为我已经为了显示所有项目,对projects_controller使用了索引操作。有人有任何建议吗?

I am having difficulty figuring out which controller/action/view to use and how to set up the routes because I am already used the index action for the projects_controller for the purpose of displaying all of the projects. Does anybody have any suggestions?

推荐答案

您可以执行/ users / {:id} / projects,它会映射到用户控制器项目动作。路线必须是自定义成员操作

You could do /users/{:id}/projects, which would map to the users controller projects action. The route would have to be custom member action

resources :users do
  member do
    get 'projects'
  end
end

这篇关于Rails 3.1-如何为同一模型组织多个索引操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆