[英]Rails nested resources routing
我正在写一个小的Ruby-on-Rails项目。 我有2个模型:用户和任务
1个任务属于两个用户(作者和目标用户),因此我在模型中做到了:
belongs_to :target, :class_name => 'User'
belongs_to :author, :class_name => 'User'
当然,用户模型有
has_many :posts
因此,我希望能够通过url / users /:username / tasks来欺骗用户的任务,而不能仅通过/ tasks来实现
所以我从routes.rb中删除了资源:tasks,并添加了嵌套资源
resources :users do
resources :tasks
end
我什至更新了视图(即edit_task_path(task)到edit_user_task_path(task))和_form.html.erb,但是出现错误:
Showing C:/Sites/todoit/app/views/tasks/index.html.erb where line #24 raised:
undefined method `task_path' for #<#<Class:0x458ce08>:0x458ad20>
我该怎么办?
<td><%= link_to 'Show', user_task_path(task) %></td>
# or
<td><%= link_to 'Show', [:user, task] %></td>
无论如何,请运行rake routes
以检查可用路线(除非您能够记住这一点)。 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.