[英]Associations in Ruby on Rails 4
I've got a question to associations in rails. 我对铁路协会提出了疑问。 I am trying to create a comment for a post which has a subject.
我正在尝试为包含主题的帖子创建评论。 So my routs file looks like this:
所以我的溃败文件看起来像这样:
resources :subjects do
resources :post do
resources :comments
end
end
Now am I trying to create a form in the show.html.erb file of the post so someone can create a comment. 现在,我正在尝试在帖子的show.html.erb文件中创建一个表单,以便某人可以创建评论。 I've tried it this way which I found in the rails guide:
我已经在rails指南中找到了这种尝试:
'posts/show.html.erb' '的帖子/ show.html.erb'
<%= form_for {[@post, @post.comments.build]} do |f| %>
//fill in form
<% end %>
'posts.controller.rb' 'posts.controller.rb'
def show
@post = Post.find(params[:id])
end
But this gives me an error. 但这给我一个错误。 If you need any other codeparts, feel free to ask.
如果您需要任何其他代码部分,请随时询问。
Error Message 错误信息
ActionView::Template::Error (undefined method `post_comments_path' for #<#<Class:0x007f9a4429d5e8>:0x007f9a42c01fc8>):
8: <strong>Text:</strong>
9: <%= @post.text %>
10: </p>
11: <%= form_for ([@post, @post.comments.build]) do |f| %>
12: <p>
13: <%= f.label :text %><br>
14: <%= f.text_area :text %>
Those paths are grouped under resources :subjects
, so your path will be subjects_post_comments_path
, or if you prefer to use polymorphic paths it will be [@subjects, @post, @post.comments.build]
. 这些路径将分组
resources :subjects
,所以你的道路将subjects_post_comments_path
,或者如果你喜欢使用多态的路径将是[@subjects, @post, @post.comments.build]
You can run rake routes | grep comments
您可以运行
rake routes | grep comments
rake routes | grep comments
to see the paths for all of your comments routes. rake routes | grep comments
以查看所有评论路线的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.