无法保存记录到数据库RAILS嵌套形式 [英] Cannot save record to database RAILS nested forms

查看:184
本文介绍了无法保存记录到数据库RAILS嵌套形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法救我的嵌套形式记录。

这是什么撬说:

 撬(#< VenuesController>)> @ venue.save
   (0.3ms)开始交易
  标签存在(为0.2ms)选择1整体从标签WHERE(标签,名=所有和标签,身份证!= 2)限制1
  标签存在(0.1毫秒)选择1整体从标签WHERE标签,名=''LIMIT 1
   (0.1毫秒)ROLLBACK TRANSACTION
=>假
 

我想我正确地遵循了一切。 这是我的嵌套形式的标签

 标签:
    <%= f.collection_check_boxes:tag_ids,Tag.all,:ID,:名称%>< BR>

  创建一个新的标签在这里:LT; BR>

   <%= f.fields_for:标签,Tag.new办| tag_field | %>
    &其中;%= tag_field.label:name_tag%GT;
    <%= tag_field.text_field:名称%>
    <%结束%GT;
 

这是我的场馆模型

 类场馆和LT;的ActiveRecord :: Base的
  的has_many:venue_tags
  的has_many:标签,:通过=> :venue_tags

  accepts_nested_attributes_for:标签,allow_destroy:真
结束
 

和我的标签模型

 类标签<的ActiveRecord :: Base的
  的has_many:venue_tags
  的has_many:场地,:通过=> :venue_tags

  validates_uniqueness_of:名称
结束
 

然而,当脱下的名字验证的唯一性',我能救它,但新的标签是由自身补充说。

这是撬日志,告诉它是真实的,但现在我得到加入到会场正确的标签,也是一个新的标签添加到会场(一个我没有创造我自己)。我假定这是因为发生了新的标签Fields_for文本是空白的,这本身创造了一个新的标签。

 (为0.2ms)开始交易
  SQL(0.6ms)INSERT INTO场所(名,地​​址,打折,纬度,经度,created_at,的updated_at)VALUES(?,?,?,?,? ,?,?)[名称,SPICEBOX],[地址,33佩尔街,纽约,NY 10013,美国],[打折,10%OFF持学生证 ],[纬度,40.714831],[经度,-73.998628],[created_at,2015年11月3日06:12:52.400643],[的updated_at,2015年11月3日06 :12:52.400643]]
  SQL(为0.2ms)INSERT INTOvenue_tags(tag_id,venue_id,created_at,的updated_at)VALUES(?,?,?,?)[tag_id,2],[venue_id ,11],[created_at,2015年11月3日06:12:52.404715],[的updated_at,2015年11月3日06:12:52.404715]]
  SQL(为0.2ms)INSERT INTO标签(名,created_at,的updated_at)VALUES(?,?,?)[名称,],[created_at,2015-11 -03 06:12:52.408821],[的updated_at,2015年11月3日06:12:52.408821]]
  SQL(0.1毫秒)INSERT INTOvenue_tags(venue_id,tag_id,created_at,的updated_at)VALUES(?,?,?,?)[venue_id,11],[tag_id ,9],[created_at,2015年11月3日06:12:52.411692],[的updated_at,2015年11月3日06:12:52.411692]]
   (1.4ms之内)提交事务
=>真正
 

解决方案

您也许应该补充一个只会验证:presence标记名称。你似乎在你的数据库还没有名字,当你没有名字再添它不是唯一的,将无法通过验证的标签。

看看表,或强烈的参数,看看该值将被清除,如果它是。

如果是因为它是故意提出这样的空白,你可以,如果空白忽略它。

  accepts_nested_attributes_for:标签,allow_destroy:真正的,:reject_if =>拉姆达{| A |一个[:名称] .blank? }
 

I cannot seem to save my record with nested forms.

This is what pry says:

pry(#<VenuesController>)> @venue.save
   (0.3ms)  begin transaction
  Tag Exists (0.2ms)  SELECT  1 AS one FROM "tags" WHERE ("tags"."name" = 'All ' AND "tags"."id" != 2) LIMIT 1
  Tag Exists (0.1ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = '' LIMIT 1
   (0.1ms)  rollback transaction
=> false

I thought I followed everything correctly. This is my nested form for tags

Tags:
    <%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %><br>

  Make a New Tag Here: <br>

   <%= f.fields_for :tags, Tag.new do |tag_field| %>
    <%= tag_field.label :name_tag %>
    <%= tag_field.text_field :name %>
    <% end %>

This is my venue model

class Venue < ActiveRecord::Base
  has_many :venue_tags
  has_many :tags, :through => :venue_tags

  accepts_nested_attributes_for :tags, allow_destroy: true
end

And my tag model

class Tag < ActiveRecord::Base
  has_many :venue_tags
  has_many :venues, :through => :venue_tags

  validates_uniqueness_of :name
end

However, when take off the 'validate uniqueness of name', I can save it, but new tags are added by itself.

And this is the pry log that tells it's true, but now I'm getting the correct tag added to the venue, but ALSO a new tag added to the venue (one I did not create myself). I am assuming this is happening because the New Tag Fields_for text was blank, which created a new tag by itself.

(0.2ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "venues" ("name", "address", "discount", "latitude", "longitude", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["name", "SPICEBOX"], ["address", "33 Pell St, New York, NY 10013, United States"], ["discount", "10% OFF with Student ID"], ["latitude", 40.714831], ["longitude", -73.998628], ["created_at", "2015-11-03 06:12:52.400643"], ["updated_at", "2015-11-03 06:12:52.400643"]]
  SQL (0.2ms)  INSERT INTO "venue_tags" ("tag_id", "venue_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["tag_id", 2], ["venue_id", 11], ["created_at", "2015-11-03 06:12:52.404715"], ["updated_at", "2015-11-03 06:12:52.404715"]]
  SQL (0.2ms)  INSERT INTO "tags" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", ""], ["created_at", "2015-11-03 06:12:52.408821"], ["updated_at", "2015-11-03 06:12:52.408821"]]
  SQL (0.1ms)  INSERT INTO "venue_tags" ("venue_id", "tag_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["venue_id", 11], ["tag_id", 9], ["created_at", "2015-11-03 06:12:52.411692"], ["updated_at", "2015-11-03 06:12:52.411692"]]
   (1.4ms)  commit transaction
=> true

解决方案

You should probably add a validates :presence to tag name. You seem to have a tag in your db that has no name and when you add another with no name it is not unique and won't pass validations.

Take a look at the form or the strong parameters to see how that value is being cleared, if it is.

If it's blank because it was intentionally submitted that way, you can ignore it if blank.

accepts_nested_attributes_for :tags, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }

这篇关于无法保存记录到数据库RAILS嵌套形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆