在Rails 5中创建嵌套资源

时间:2018-11-17 23:38:22

标签: 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>:0x00007fb361eab868>

这是它所指的行:

<%= simple_form_for [@product, @note] do |f| %>

这是Notes控制器中的新动作和创建动作:

  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"}

2 个答案:

答案 0 :(得分:1)

localhost:3000/login?locale=en@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 notes#index

答案 1 :(得分:1)

参考您的修改;当然,您应该会得到空字段错误,因为您正在创建新对象@note而没有为其提供任何属性:

  @note = @product.notes.new

应该像

@note = @product.notes.build(params[:note])

还要注意在Notes控制器中为笔记提供消毒剂:

private 

def note_params
   params.require(:note).permit(:content, :author, :visible, :product_id)
end

因此您创建的代码将如下所示:

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