Cannot save record to database RAILS nested forms

假如想象 提交于 2019-12-13 05:19:29

问题


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

回答1:


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



回答2:


However, when take off the 'validate uniqueness of name', I can save it

Check the database for the name and see if it already exists. Since you are able to save the record, you code is probably ok.

To see why the record doesn't want to save, you can ask out the errors of the object you are trying to save. You can do this in your pry console.

@venue.save
...
=> false

@venue.errors.messages
=> ...

It will probably tell you that you already have a record with the same name. If that it's the case, then it would make sense your record isn't saving.



来源:https://stackoverflow.com/questions/33492570/cannot-save-record-to-database-rails-nested-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!