【发布时间】:2013-12-06 15:14:51
【问题描述】:
我有一个名为 Schedule 的模型,它属于一个项目。一个项目有一个时间表。我正在尝试遵循 CRUD 约定,但遇到了一些困难。我有一个列出所有项目的页面,并且每个项目旁边都有一个链接来创建时间表。我从我的路由文件中的以下内容开始:
resources :schedules
这就是问题所在。在“新日程”页面的 url 中,需要有一个 :id 表示日程所属的项目,这样当创建日程时,它将属于正确的项目。我不知道如何使用资源来做到这一点,所以我将路线代码更改为:
match 'schedules/new/:id', to: 'schedules#new', as: :new_schedule, via: [:get, :post]
resources :schedules, except: [:new, :create]
由于某种原因,此页面是空白的。它只是白色的。如何修复我的路线?谢谢。
更新:
我还尝试将路线更改为以下内容:
resources :projects do
resources :schedules
end
这使得新日程的 url 采用以下形式:
/projects/:project_id/schedules/new(.:format)
我认为应该这样做,但是,新时间表的表格写成
form_for @schedule
这会产生以下错误:
undefined method `schedules_path'
有什么想法吗?
【问题讨论】:
标签: ruby-on-rails routes ruby-on-rails-4 rails-routing activeresource