【问题标题】:Creation of nested routes创建嵌套路由
【发布时间】:2013-02-14 17:16:08
【问题描述】:

在我的应用程序中,我的项目有票。我希望能够通过以下方式出示门票:

/projects/43/tickets

此路线有效,但会显示所有工单,而不仅仅是属于该项目的工单。我需要改变什么?

路线:

resources :projects do
  resources :tickets do
    collection do
      get "manage"
    end 
  end
end

型号:

class Ticket < ActiveRecord::Base
  belongs_to :project
  ...
end

class Project < ActiveRecord::Base
  has_many :tickets, :dependent => :destroy
  ...
end

工单通过以下方式连接到项目:

Tickets (table)
    project_id
    ...the rest of the fields...

【问题讨论】:

    标签: ruby-on-rails database ruby-on-rails-3 routes


    【解决方案1】:

    这是控制器逻辑,而不是路由逻辑。在tickets_controller.rb

    def index
        @project = Project.find(params[:project_id])
        @tickets = @project.tickets
    end
    

    现在,@tickets 将包含当前项目的所有票证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2022-08-17
      相关资源
      最近更新 更多