我似乎無法使用嵌套窗體保存我的記錄。無法將記錄保存到數據庫RAILS嵌套窗體
這就是撬說:
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
我認爲我正確地遵循了一切。 這是我的標籤
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 %>
嵌套形式這是我的場館模型
class Venue < ActiveRecord::Base
has_many :venue_tags
has_many :tags, :through => :venue_tags
accepts_nested_attributes_for :tags, allow_destroy: true
end
我的標籤模型
class Tag < ActiveRecord::Base
has_many :venue_tags
has_many :venues, :through => :venue_tags
validates_uniqueness_of :name
end
然而,脫下「的名字驗證唯一性」時,我可以保存它,但新的標籤本身添加。
這是一個告訴它是真實的pry日誌,但現在我得到了添加到場地的正確標籤,但同時還添加了一個新標籤(一個我沒有創建自己)。我假設這是因爲New Tag Fields_for文本是空白的,它自己創建了一個新標籤。
(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
太棒了!現在起作用了。謝謝! – minlingzhao