通过表单 [Rails 5] 提交嵌套资源时遇到问题
Having trouble submitting nested resource through form [Rails 5]
我有一个Document
,has_many
Section
,每个section
has_one
Comment
。我希望能够在 Document
show
视图中同时创建 sections
和 comments
,但是我无法让 comments
通过。
这是我得到的最接近的相关代码:
class CommentsController < ApplicationController
def create
@section = Section.find(params[:id])
@section.comment.create(comment_params)
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
路由:
resources :documents, shallow: true do
resources :sections do
resources :comments
end
end
以及具有以下形式的视图:
# app/views/documents/show.html.erb
<% @document.sections.each do |section| %>
<%= section.body %>
<% if section.comment %>
<p>
<%= section.comment %>
</p>
<% else %>
<%= form_with url: section_comments_path(section.id), scope: 'comment' do |form| %>
<%= form.text_field :body, placeholder: "Comment" %>
<%= form.submit %>
<% end %>
<% end %>
<% end %>
这一切似乎都在为我检查,但是当我尝试 post 发表评论时,我得到的是:
Started POST "/sections/51/comments" for ::1 at 2019-05-24 23:29:06 +0000
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>[...], "comment"=>{"body"=>"asdas"}, "commit"=>"Save comment", "section_id"=>"51"}
Section Load (0.5ms) SELECT "sections".* FROM "sections" WHERE "sections"."id" = ? LIMIT ? [["id", 51], ["LIMIT", 1]]
comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."section_id" = ? LIMIT ? [["section_id", 51], ["LIMIT", 1]]
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.9ms)
NoMethodError (undefined method `create' for nil:NilClass):
app/controllers/comments_controller.rb:4:in `create'
有什么想法吗?
一个has_one
关系returns对象本身。因此,@section.comment.create(comment_params)
将不起作用,因为 @section.comment
为零。相反,尝试类似...
def create
@section = Section.find(params[:section_id])
@comment = Comment.create(comment_params)
@section.comment = @comment
...
end
或者,如 Rails 指南中所述...
When initializing a new has_one or belongs_to association you must use
the build_ prefix to build the association, rather than the
association.build method that would be used for has_many or
has_and_belongs_to_many associations. To create one, use the create_
prefix.
看起来像这样
def create
@section = Section.find(params[:section_id])
@section.create_comment(comment_params)
...
end
您可能需要更改:
@section.comment.create(comment_params)
至:
@section.comments.create(comment_params)
如果还是不行,试试:
@section.comment.create!(comment_params)
看看异常是怎么说的
我有一个Document
,has_many
Section
,每个section
has_one
Comment
。我希望能够在 Document
show
视图中同时创建 sections
和 comments
,但是我无法让 comments
通过。
这是我得到的最接近的相关代码:
class CommentsController < ApplicationController
def create
@section = Section.find(params[:id])
@section.comment.create(comment_params)
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
路由:
resources :documents, shallow: true do
resources :sections do
resources :comments
end
end
以及具有以下形式的视图:
# app/views/documents/show.html.erb
<% @document.sections.each do |section| %>
<%= section.body %>
<% if section.comment %>
<p>
<%= section.comment %>
</p>
<% else %>
<%= form_with url: section_comments_path(section.id), scope: 'comment' do |form| %>
<%= form.text_field :body, placeholder: "Comment" %>
<%= form.submit %>
<% end %>
<% end %>
<% end %>
这一切似乎都在为我检查,但是当我尝试 post 发表评论时,我得到的是:
Started POST "/sections/51/comments" for ::1 at 2019-05-24 23:29:06 +0000
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>[...], "comment"=>{"body"=>"asdas"}, "commit"=>"Save comment", "section_id"=>"51"}
Section Load (0.5ms) SELECT "sections".* FROM "sections" WHERE "sections"."id" = ? LIMIT ? [["id", 51], ["LIMIT", 1]]
comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."section_id" = ? LIMIT ? [["section_id", 51], ["LIMIT", 1]]
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.9ms)
NoMethodError (undefined method `create' for nil:NilClass):
app/controllers/comments_controller.rb:4:in `create'
有什么想法吗?
一个has_one
关系returns对象本身。因此,@section.comment.create(comment_params)
将不起作用,因为 @section.comment
为零。相反,尝试类似...
def create
@section = Section.find(params[:section_id])
@comment = Comment.create(comment_params)
@section.comment = @comment
...
end
或者,如 Rails 指南中所述...
When initializing a new has_one or belongs_to association you must use the build_ prefix to build the association, rather than the association.build method that would be used for has_many or has_and_belongs_to_many associations. To create one, use the create_ prefix.
看起来像这样
def create
@section = Section.find(params[:section_id])
@section.create_comment(comment_params)
...
end
您可能需要更改:
@section.comment.create(comment_params)
至:
@section.comments.create(comment_params)
如果还是不行,试试:
@section.comment.create!(comment_params)
看看异常是怎么说的