我有一个课程和教学大纲模型定义如下
class Course < ActiveRecord::Base
has_many :enrollments
has_many :students, through: :enrollments, class_name: "User"
has_many :syllabuses
end
class Syllabus < ActiveRecord::Base
belongs_to :course
end
像这样定义的路线
resources :courses do
resource :syllabuses
end
我的html有一个编辑教学大纲的链接
<td><%= link_to "Edit", edit_course_syllabuses_path, class: "btn btn-primary" %></td>
rake路线向我展示了这个
edit_course_syllabuses GET /courses/:course_id/syllabuses/edit(.:format) syllabuses#edit
服务器日志显示请求处理,如
Started GET "/courses/46/syllabuses/edit" for ::1 at 2015-02-04 20:55:43 +0530
Processing by SyllabusesController#edit as HTML
Parameters: {"course_id"=>"46"}
Syllabus Load (0.3ms) SELECT "syllabuses".* FROM "syllabuses" WHERE "syllabuses"."id" = $1 LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms
ActiveRecord::RecordNotFound - Couldn't find Syllabus with 'id'=:
如何将课程提纲ID传递给教学大纲控制器编辑操作?
答案 0 :(得分:0)
由于您有has_many
关联,因此您还需要resources :syllabuses
(请注意 s )。
更改它,重新启动服务器并运行以下代码以查看所有路由:
bundle exec rake routes
您的路径现在是edit_course_syllabuse
,您可以像以下一样使用它:
<%= link_to "Edit", edit_course_syllabuses_path(@syllabuse), class: "btn btn-primary" %>
您可以在Raild guide
中详细了解嵌套资源