Rails嵌套资源路由

时间:2013-11-11 00:23:29

标签: ruby-on-rails ruby model-view-controller rails-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来CRUD用户的任务,而不能只通过/ tasks

来完成它

所以我删除了来自routes.rb的资源:任务并添加了嵌套资源

    resources :users do
      resources :tasks
    end

我甚至更新了视图(例如edit_task_path(task)to 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>

我该怎么办?

<小时/> 发布index.html.erb:http://pastebin.com/DdGurSfe

1 个答案:

答案 0 :(得分:1)

<td><%= link_to 'Show', user_task_path(task) %></td>
# or
<td><%= link_to 'Show', [:user, task] %></td>

无论如何运行rake routes来检查可用的路线(除非你能够记住它)。干杯!