Ruby on rails 在Rails 5中创建嵌套资源
我正在尝试创建一个嵌套资源,以便产品可以有与之关联的注释。我已在模型等中设置了关联,但当我尝试使用表单创建新便笺时,出现以下错误:Ruby on rails 在Rails 5中创建嵌套资源,ruby-on-rails,ruby-on-rails-5,simple-form,nested-forms,Ruby On Rails,Ruby On Rails 5,Simple Form,Nested Forms,我正在尝试创建一个嵌套资源,以便产品可以有与之关联的注释。我已在模型等中设置了关联,但当我尝试使用表单创建新便笺时,出现以下错误: 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
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| %>
<%= 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 %>
当我之前收到错误时,它将此作为请求参数,所以它们被传递了
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
重定向到
应该是产品注释路径(@product)
注释#索引@product
在创建中为零
您的表单未通过验证和呈现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_路径(@product)
notes#index引用您的编辑;当然,您应该得到空字段错误,因为您正在创建一个新对象@note
,但没有为它提供任何属性:
@note = @product.notes.new
应该是
@note = @product.notes.build(params[:note])
还要注意为notes控制器中的notes提供消毒剂:
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
参考你的编辑;当然,您应该得到空字段错误,因为您正在创建一个新对象@note
,但没有为它提供任何属性:
@note = @product.notes.new
应该是
@note = @product.notes.build(params[:note])
还要注意为notes控制器中的notes提供消毒剂:
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
好了,谢谢你们的回答。我正在努力寻找关于我应该在控制器中为嵌套资源使用什么的文档。谢谢你的帮助!好了,谢谢你们的回答。我正在努力寻找关于我应该在控制器中为嵌套资源使用什么的文档。谢谢你的帮助!