Tag: 嵌套属性

Ruby on Rails嵌套属性没有保存到数据库中?

我正在尝试在“Todo列表”中创建项目列表,但是,我不确定我是否正确使用嵌套属性执行此操作。 我认为使用嵌套属性是正确的尝试,因为会有一个大的项目列表,并且它将与基于ids的正确“待办事项列表”相关联。 填充记录时表格的示例 待办事项表 id list 1 grocery shopping 2 health insurance 物品表 id todo_id name 1 1 buy milk 2 1 buy cereal 3 2 Blue Shield 4 2 Healthnet 5 1 buy cherries 虽然我的尝试如下,但我的应用程序没有将任何数据保存到Item数据库中。 Todo控制器 class TodoController < ApplicationController def new @todo = Todo.new @todo.items.build end end Todo模型 class Todo < ActiveRecord::Base belongs_to […]

As1851Tests中的NoMethodError#new with shallow routes

我有一个使用浅层资源嵌套的rails 5应用程序。 form_for已停止工作,我找不到原因。 该应用程序有多个嵌套模型,在根据我之前在此处提出的问题的建议更改为浅路线之前有效 我的架构如下: DB / schema.rb: create_table “as1851_tests”, force: :cascade do |t| t.bigint “damper_id” t.boolean “AS1851A”, default: false, null: false t.boolean “AS1851B”, default: false, null: false t.boolean “AS1851C”, default: false, null: false t.boolean “AS1851D”, default: false, null: false t.boolean “AS1851E”, default: false, null: false t.boolean “Pass”, default: false, null: false t.string “comment” t.datetime […]

合并嵌套属性

我有一个使用post,评论和作者模型的样本。 请从以下主题中查看我的完整要求 嵌套路由和CRUD操作以及Rails中的附加值 在这里,我需要在创建post时创建一个注释。 我使用嵌套属性并且工作正常。 但我的问题是我不想要用户的所有属性。 某些属性需要在内部添加。 如果属性没有嵌套属性,我可以合并。 但是合并不适用于嵌套属性。 我从这里得到了解决方案 现在我的params方法如下所示; private def create_params filtered_params = params.require(:post) .permit([:name, :description, comments_attributes: [:author_id, :comments]]) additional_params = {record_status: 1, comments_attributes: [record_status: 1]} result = filtered_params.merge(additional_params) do |key, oldval, newval| if newval.is_a? Array oldval ||= {} Hash[oldval.map {|k, v| [k, v.merge(newval.first)]}] elsif newval.is_a? Hash oldval ||= {} oldval.merge newval […]

Rails 4 has_many nested_attributes来替换所有

我想使用nested_attributes来替换所有旧的关联对象。 最好的方法是怎样做的? 如果我使用下面的代码,每次使用nested_attributes更新主对象时,都会创建关联的新对象。 accepts_nested_attributes_for :days 编辑 我在下面的before_validation回调中得到了它: def replace_days db_days = days.where(‘id IS NOT NULL’) all_days = days all_days -= db_days self.days = all_days end 问题在于我对子模型的独特validation。 Rails将进入数据库以validation唯一性。 由于在validation时我的替换不在数据库上,因此得到了validation错误。 不知道如何绕过这个,如果这是最好的方法。 我希望rails有一些本机function或参数可以帮助我完全按照自己的意愿行事。

“无法使用嵌套的受保护模型批量分配受保护的属性”

我有时间试图让这个嵌套模型正常工作。 我已经尝试了所有方式的复数/单数,完全删除了attr_accessible,谁知道还有什么。 restaurant.rb: # == RESTAURANT MODEL # # Table name: restaurants # # id :integer not null, primary key # name :string(255) # created_at :datetime not null # updated_at :datetime not null # class Restaurant :jobs has_many :positions accepts_nested_attributes_for :jobs, :allow_destroy => true validates :name, presence: true end job.rb: # == JOB MODEL # […]

rails 4 collection_select多个嵌套属性未正确保存

我是rails的新手,我在使用collection_select保存连接表的嵌套属性时遇到问题。 我有模型post,tag和post_tagging。 post_tagging是一个连接表。 我想为每个post设置多个标签,所以我尝试通过collection_select使用多选,但是当我保存时,只将post_id插入到数据库中。 下面是我的代码和日志。 Post.rb class Post < ActiveRecord::Base has_many :post_taggings, foreign_key: :post_id, dependent: :destroy has_many :tags, through: :post_taggings, source: :tag accepts_nested_attributes_for :post_taggings, reject_if: :all_blank, allow_destroy: true end Tag.rb class Tag < ActiveRecord::Base has_many :post_taggings, foreign_key: :tag_id, dependent: :destroy has_many :posts, through: :post_taggings, source: :post end post_tagging.rb(我在post_tagging模型中关闭了tag_id和post_id的状态validation,因此我可以获得POST的日志。) class PostTagging < ActiveRecord::Base belongs_to :post belongs_to […]

validation表单中是否存在嵌套属性

我有以下协会: #models/contact.rb class Contact < ActiveRecord::Base has_many :contacts_teams has_many :teams, through: :contacts accepts_nested_attributes_for :contacts_teams, allow_destroy: true end #models/contacts_team.rb class ContactsTeam < ActiveRecord::Base belongs_to :contact belongs_to :team end #models/team.rb class Team < ActiveRecord::Base has_many :contacts_team has_many :contacts, through: :contacts_teams end contact应始终至少有一个关联的团队(在contacts_teams的富联接表中指定)。 如果用户尝试创建没有关联团队的联系人:应该抛出validation。 如果用户尝试删除所有联系人的关联团队:应该抛出validation。 我怎么做? 我确实查看了嵌套属性文档。 我也看过这篇文章和这篇文章都有点过时了。 完成:我使用nested_form_fields gem动态地向联系人添加新的关联团队。 以下是表单上的相关部分(有效,但目前尚未validation至少有一个团队与该联系人关联): 因此,当未单击“添加团队”时,没有任何内容通过与团队相关的参数传递,因此不会创建contacts_team记录。 但是当点击“添加团队”并选择一个团队并提交表单时,这样的事情会通过参数传递: “contacts_teams_attributes”=>{“0″=>{“team_id”=>”1”}}

在视图中没有显示fields_for has_many关联的复数

目前, Item 属于一个公司并且has_many ItemVariants 。 我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示表单。 它仅在我使用单数时显示。 我检查了我的关联,它们似乎是正确的,它可能与项目嵌套在公司下面有关,还是我错过了其他的东西? 提前致谢。 注意:下面的代码段中省略了不相关的代码。 编辑:不知道这是否相关,但我正在使用CanCan进行身份validation。 的routes.rb resources :companies do resources :items end item.rb的 class Item < ActiveRecord::Base attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes # Associations #———————————————————————– belongs_to :company belongs_to :item_type has_many :item_variants accepts_nested_attributes_for :item_variants, allow_destroy: true end item_variant.rb class ItemVariant < ActiveRecord::Base attr_accessible :item_id, :location_id # Associations #———————————————————————– belongs_to […]

simple_form中的嵌套属性返回质量分配错误

楷模: class Topic :destroy validates :name, :presence => true, :length => { :maximum => 32 } attr_accessible :name, :post_id end class Post true has_many :comments, :dependent => :destroy accepts_nested_attributes_for :topic attr_accessible :name, :title, :content, :topic, :topic_attributes end 视图: { :controller => :posts, :action => “create” } do |f| %> Create a Post false, :placeholder => […]

从嵌套表单发出combobox中的更新类

我创建了一个combobox,显示供应商,因此当选择供应商时将更新显示购买的div,显示购买后将以comboboxforms进行嵌套,因此当我选择购买时将显示所选购买的金额。 问题是,当我选择购买时,仅在第一行中工作,当我选择另一个combobox线时,显示所选择的第一个combobox的数量。 在这里我的表 suppliers |id| |name| 1 Supplier A 2 Supplier B shopping_documents |id| |supplier_id| 1 1 2 2 shopping_products |id| |shopping_document_id| |qty| |purchase_product_id| 1 1 1 1 2 1 1 2 purchase_products |id| |name| |price_sale| 1 XP 1000 2 VISTA 2000 supplier_products |id| |supplier_id| |amount| |purchase_product_id 1 1 1000 1 2 1 2000 1 […]