Rails 5使用Cocoon gem在Ruby-on-Rails中使用嵌套表单保存到我的表中

时间:2016-09-09 02:27:05

标签: mysql ruby-on-rails nested-forms cocoon-gem

RAILS 5问题。我有一个项目,我希望用户可以选择向表单添加额外的项目。我的代码正确呈现,添加更多字段的链接确实添加了更多的文本字段。我遇到的问题是没有保存到我的数据库。我在MySQl数据库上使用mysql2 gem。我发布了控制台输出。这就是我所拥有的:

forms_controller

  # forms_controller.rb
  def new
    @form = Form.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @form }
    end
  end
  def create
    @form = Form.new(form_params)

    respond_to do |format|
      if @form.save
        format.html { redirect_to(@form, :notice => 'form was successfully created.') }
        format.xml  { render :xml => @form, :status => :created, :location => @form }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @form.errors, :status => :unprocessable_entity }
      end
    end
  end

  def form_params
    params.require(:form).permit(:name, items_attributes: [:id, :item_name, :_destroy])
  end

模型

# form.rb
class Form < ApplicationRecord
  has_many :items
  accepts_nested_attributes_for :items, reject_if: :all_blank, allow_destroy: true
  validates_presence_of :name
end

# item.rb
class Item < ApplicationRecord
   belongs_to :form
end

的观点

<!-- _form.html.haml -->

= form_for @form do |f|
  .field
    = f.label :name
    %br
    = f.text_field :name
  %h3 items
  #items
    = f.fields_for :items do |item|
      = render 'item_fields', f: item
    .links
      = link_to_add_association 'add item', f, :items
  = f.submit
  
.nested-fields
  .field
    = f.label :item_name
    %br
    = f.text_field :item_name
  = link_to_remove_association "remove item", f

控制台

tarted POST "/forms" for 127.0.0.1 at 2016-09-08 23:04:17 -0600
Processing by FormsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FmYPwlXy93iwBMkWvfyd3QJ+NKDcYnraUaKmMISbUIX3+KRL7KtpD33FW3CK+jxvn4AoUUMhx4zrGncJej5BOw==", "form"=>{"name"=>"sfsf", "items_attributes"=>{"1473397456358"=>{"item_name"=>"sfsf", "_destroy"=>"false"}}}, "commit"=>"Create Form"}
   (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
  Rendering forms/new.html.haml within layouts/application
  Rendered forms/_item_fields.html.haml (1.5ms)
  Rendered forms/_item_fields.html.haml (1.2ms)
  Rendered forms/_form.html.haml (8.5ms)
  Rendered forms/new.html.haml within layouts/application (10.1ms)
Completed 200 OK in 68ms (Views: 54.5ms | ActiveRecord: 0.5ms)

3 个答案:

答案 0 :(得分:2)

在您的许可/要求行(在您的控制器中),item_attribute应该是item_attributes(不止一个),即:

def form_params
  params.require(:form).permit(:name, :date_sent, :quantity, :comment, item_attributes: [:id, :name, :form_id, :_destroy])
end

答案 1 :(得分:2)

在Rails 5中,每当我们定义belongs_to关联时,都需要在此更改后默认存在关联记录。

如果不存在相关记录,则会触发验证错误。

class User < ApplicationRecord
end

class Post < ApplicationRecord
  belongs_to :user
end

post = Post.create(title: 'Hi')
=> <Post id: nil, title: "Hi", user_id: nil, created_at: nil, updated_at: nil>

post.errors.full_messages.to_sentence
=> "User must exist"

****在Rails 5 ****

中选择退出此默认行为

我们可以将可选的:true传递给将删除此验证检查的belongs_to关联。

class Post < ApplicationRecord

  belongs_to :user, optional: true

end

post = Post.create(title: 'Hi')

=> <Post id: 2, title: "Hi", user_id: nil>

答案 2 :(得分:1)

它不会保存的原因是因为我使用的是Rails 5,我需要将optional: true添加到项目模型中的belongs_to。

# form.rb
class Form < ApplicationRecord
  has_many :items
  accepts_nested_attributes_for :items, reject_if: :all_blank, allow_destroy: true
  validates_presence_of :name
end

# item.rb
class Item < ApplicationRecord
   belongs_to :form , optional: true
end