在 Rails 中创建嵌套资源 5
Creating a nested resource in Rails 5
我正在尝试创建一个嵌套资源,以便产品可以有与之关联的注释。我已经在模型等中设置了关联,但是当我尝试使用表单创建新笔记时,出现以下错误:
NoMethodError in Notes#create
Showing /Users/myusername/myapp/app/views/notes/_form.html.erb where line #2 raised:
undefined method `notes_path' for #<#<Class:0x00007fb3630b1ad0>:0x00007fb361eab868>
这是它所指的行:
<%= simple_form_for [@product, @note] do |f| %>
这是笔记控制器中的新建和创建操作:
def new
@product = Product.find(params[:product_id])
@note = @product.notes.build
end
def create
@note = Note.new(product: @product)
respond_to do |format|
if @note.save
format.html { redirect_to product_notes, notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
和部分形式:
<%= simple_form_for [@product, @note] do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.input :content %>
<%= f.input :author %>
<%= f.check_box :visible %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
我正在兜圈子进行更改,似乎找不到任何关于未弃用的嵌套资源的文档。有人可以帮忙吗?
编辑添加:
我根据 PGill 的回答更改了我的控制器操作,现在可以在没有操作控制器错误的情况下加载页面。但是,它现在重新呈现新的注释表单,错误提示表单字段不能为空。我提交的时候它们不是空白的 - 是什么原因导致的?
更新控制器操作:
def create
@product = Product.find(params[:product_id])
@note = @product.notes.new
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
format.html { render :new, notice: 'Note failed to be created.' }
end
end
end
当我之前遇到错误时,它把这个作为请求参数,所以它们被传递了吗?
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"lotsofletters",
"note"=>{"content"=>"test", "author"=>"test", "visible"=>"0"},
"commit"=>"Create Note",
"product_id"=>"1"}
@product
在 create
中为零
您的表单未通过验证和呈现 new
将创建操作更新为
def create
@product = Product.find(params[:product_id])
@note = @product.notes.new
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
redirect_to
应该是 product_notes_path(@product)
notes#index
指的是您的编辑;当然你应该得到空字段错误,因为你正在创建一个新对象 @note
而没有为其提供任何属性:
@note = @product.notes.new
应该是
@note = @product.notes.build(params[:note])
还要注意在笔记控制器中为笔记提供消毒剂:
private
def note_params
params.require(:note).permit(:content, :author, :visible, :product_id)
end
因此您在 create 中的代码将如下所示:
def create
@product = Product.find(params[:product_id])
@note = @product.notes.build(note_params)
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
private
def note_params
params.require(:note).permit(:content, :author, :visible, :product_id)
end
我正在尝试创建一个嵌套资源,以便产品可以有与之关联的注释。我已经在模型等中设置了关联,但是当我尝试使用表单创建新笔记时,出现以下错误:
NoMethodError in Notes#create
Showing /Users/myusername/myapp/app/views/notes/_form.html.erb where line #2 raised:
undefined method `notes_path' for #<#<Class:0x00007fb3630b1ad0>:0x00007fb361eab868>
这是它所指的行:
<%= simple_form_for [@product, @note] do |f| %>
这是笔记控制器中的新建和创建操作:
def new
@product = Product.find(params[:product_id])
@note = @product.notes.build
end
def create
@note = Note.new(product: @product)
respond_to do |format|
if @note.save
format.html { redirect_to product_notes, notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
和部分形式:
<%= simple_form_for [@product, @note] do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.input :content %>
<%= f.input :author %>
<%= f.check_box :visible %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
我正在兜圈子进行更改,似乎找不到任何关于未弃用的嵌套资源的文档。有人可以帮忙吗?
编辑添加:
我根据 PGill 的回答更改了我的控制器操作,现在可以在没有操作控制器错误的情况下加载页面。但是,它现在重新呈现新的注释表单,错误提示表单字段不能为空。我提交的时候它们不是空白的 - 是什么原因导致的?
更新控制器操作:
def create
@product = Product.find(params[:product_id])
@note = @product.notes.new
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
format.html { render :new, notice: 'Note failed to be created.' }
end
end
end
当我之前遇到错误时,它把这个作为请求参数,所以它们被传递了吗?
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"lotsofletters",
"note"=>{"content"=>"test", "author"=>"test", "visible"=>"0"},
"commit"=>"Create Note",
"product_id"=>"1"}
@product
在 create
您的表单未通过验证和呈现 new
将创建操作更新为
def create
@product = Product.find(params[:product_id])
@note = @product.notes.new
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
redirect_to
应该是 product_notes_path(@product)
notes#index
指的是您的编辑;当然你应该得到空字段错误,因为你正在创建一个新对象 @note
而没有为其提供任何属性:
@note = @product.notes.new
应该是
@note = @product.notes.build(params[:note])
还要注意在笔记控制器中为笔记提供消毒剂:
private
def note_params
params.require(:note).permit(:content, :author, :visible, :product_id)
end
因此您在 create 中的代码将如下所示:
def create
@product = Product.find(params[:product_id])
@note = @product.notes.build(note_params)
respond_to do |format|
if @note.save
format.html { redirect_to product_notes_path(@product), notice: 'Note was successfully created.' }
else
flash.now[:error] = "It doesnt work"
render 'new'
end
end
end
private
def note_params
params.require(:note).permit(:content, :author, :visible, :product_id)
end