我知道你可以:
accepts_nested_attributes_for :foo, :reject_if => proc { |a| a[:bar].blank? }
Run Code Online (Sandbox Code Playgroud)
有没有办法说出类似的话
accepts_nested_attributes_for :foo, :reject_if => blah[:bar].blank? and flah[:bar].blank?
Run Code Online (Sandbox Code Playgroud)
要么
accepts_nested_attributes_for :foo, :reject_if => all fields except record_date.blank?
Run Code Online (Sandbox Code Playgroud)
谢谢
我目前有一个使用Cocoon生成的输入表单,并希望使其创建的输入字段可排序(使用jQuery-ui中的sortable).
嵌套表单的代码是:
<%= f.simple_fields_for :checkout_procedures do |procedure| %>
<li><%= render 'checkout_procedure_fields', :f => procedure %></li>
<% end %>
<div class="links">
<%= link_to_add_association 'add step', f, :checkout_procedures %>
</div>
Run Code Online (Sandbox Code Playgroud)
渲染的部分(_checkout_procedure_fields)是:
<div class="checkout_procedure nested-fields">
<table>
<tr>
<td><%= f.input :step %></td>
<td><%= link_to_remove_association "remove step", f %></td>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以通过将现有元素置于a中<div>并使用jQuery 将其设置为可排序来使现有元素可排序:
<div class="sortThese">
<%= f.simple_fields_for :checkout_procedures do |procedure| %>
<li><%= render 'checkout_procedure_fields', :f => procedure %></li>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'add step', f, :checkout_procedures %>
</div> …Run Code Online (Sandbox Code Playgroud) jquery ruby-on-rails jquery-ui-sortable nested-attributes cocoon-gem
我有一个嵌套的表单(使用Ryan B的nested_form gem),使用has_and_belongs_to_many来设置has_and_belongs_to_many:
Opening has_and_belongs_to_many :contacts
Contact has_and_belongs_to_many :openings
在尝试向开口添加新联系人时,在这种情况下,我得到:
Can't mass-assign protected attributes: new_1346666966632
对于
"opening"=>{"contacts_attributes"=>{"new_1346666966632"=>{"contacts"=>{"name"=>"Test Contact",
我添加了相应的"accepts_nested_attributes_for"和"attr_accessible",并在控制器中构建了联系人,即@ opening.contacts.build和@ opening.contacts.build(params [:opening] [:contact_attributes]).
我哪里错了?在这里使用has_many通关系会更好吗?
编辑:
视图:
<%= simple_nested_form_for @opening, :wrapper => :plain do |f| %>
<%= f.link_to_add "Add a contact", :contacts %>
<%= f.button :submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
其中使用partial为嵌套联系人生成字段:
<%= f.fields_for :contacts, @opening.contacts.build do |contact_form| %>
<%= contact_form.input :name, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :company, :label => false, :input_html => { …Run Code Online (Sandbox Code Playgroud) ruby-on-rails nested-forms has-and-belongs-to-many nested-attributes ruby-on-rails-3
目前,Item 属于一个公司并且has_many ItemVariants.
我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示表单.它仅在我使用单数时显示.
我检查了我的关联,它们似乎是正确的,它可能与项目嵌套在公司下面有关,还是我错过了其他的东西?
提前致谢.
注意:下面的代码段中省略了不相关的代码.
编辑:不知道这是否相关,但我正在使用CanCan进行身份验证.
的routes.rb
resources :companies do
resources :items
end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
item_variant.rb
class ItemVariant < ActiveRecord::Base
attr_accessible :item_id, :location_id
# Associations
#-----------------------------------------------------------------------
belongs_to :item
end
Run Code Online (Sandbox Code Playgroud)
项目/ new.html.erb
<%= form_for [@company, @item] do |f| %>
...
...
<%= f.fields_for :item_variants do |builder| %>
<fieldset>
<%= …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails nested-attributes fields-for ruby-on-rails-3
我有一个要求,如何开始,我寻求一些帮助有点困难.我有三个表,即服装,类别和材料.考虑服装表中包含的内容
-----男装
-----童装
我有一个页面来添加服装,而添加服装我需要有一个下拉列表,应列出类别.在选择类别时,属于所选类别的材料应出现在多选框中,我们可以从中选择必须保存在表格中的多种材料.
选择类别的下拉列表应该嵌套,因为我们也可以选择多个类别,每次添加类别时,应该在类别下拉列表后显示与该类别相关的多个选择下拉列表.
请考虑以下清楚解释的图像

如何创建一个表来保存我从这些表中选择的值?
更新:
class Apparel < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :apparels
has_and_belongs_to_many :materials
end
class Material < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Run Code Online (Sandbox Code Playgroud)
以上是这些之间的模型和关联.我想要显示一个下拉菜单,并且应该包含类别以及更多这个下拉列表,当选中时,每个下拉列表下方应显示多选框以从中选择材料或告诉我是否可以像保持多个一样 - 选择而不是下拉,并在每个选择另一个多选框时应填充与其相关的值.以下图片将清楚地解释

ruby-on-rails nested-attributes ruby-on-rails-3 drop-down-menu
我一直试图找到这个链接几个小时了.我有一个多态关联,其中两个集合和分类都有设计.
收集模型
has_many :designs, :as => :targetable
Run Code Online (Sandbox Code Playgroud)
分类模型
has_many :designs, :as => :targetable
Run Code Online (Sandbox Code Playgroud)
设计模型
belongs_to :targetable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
为了链接到设计的'show'动作,正确的多态路径将是:
link_to polymorphic_path([@targetable, @design])
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何链接到设计的"索引"页面,以显示与其相应的可定位对象相关的所有设计.
有谁知道到达那里的适当链接?
ruby-on-rails polymorphic-associations nested-attributes ruby-on-rails-4
我使用 simple_form。使用 rails accepts_nested_attributes_for 时,表单验证失败,抱怨嵌套属性验证失败,因为父对象的 id 不能为空 (validates_presence_of)。
我知道正在提交父对象 id,因为如果我删除表单提交的验证,并且子记录是有效的。因此,在父 ID 与子 ID 关联之前,验证正在查看参数……因此失败。这看起来很奇怪。为什么 rails 在嵌套属性的父级正确关联之前的表单提交阶段运行验证?
有没有处理这种情况的方法?
我目前正在尝试使用best_in_place gem来在HTML表格中进行内联编辑.我在购物车的展示视图中显示了一个购物车.在购物车的展示视图中,我可以添加lineItems.创建LineItem时,还会使用lineItem_id创建新的可用记录,然后在购物车中显示其lineitem.Cart和LineItem表都来自外部数据库,因此我无法添加列,这就是为什么我不能只为LineItem添加一个可用的布尔属性.
**cart.rb
class Cart << AR::Base
has many LineItems
end
**line_item.rb
class LineItems <<AR::Base
belongs_to Cart
has_one :available
accepts_nested_attributes_for :available
end
**available.rb
class Available<<AR::Base
belongs_to LineItems
end
**views/cart/show.html.erb
@cart.lineitems.each do |line_items|
<td><%= line_item.price %></td>
<td><%=line_item.name %></td>
<td><%= best_in_place line_item.available.boolean, :boolean, :path => line_items_path, :type => type: :checkbox, collection: %w[No Yes] %></td>
end
Run Code Online (Sandbox Code Playgroud)
我希望能够使用best_in_place编辑html表中的line_item.available.boolean,该表位于购物车展示视图中,但我没有运气..任何帮助都会令人惊叹!=]我知道在阅读之后,使用嵌套属性是不可能的,但是如果我能以某种方式摆脱可用模型并在show table中有一个字段,我可以为line_item编辑以查看lineItem是否可用那也很棒.我对任何想法持开放态度!
ruby-on-rails nested-attributes best-in-place ruby-on-rails-4
我有一个可以有多个 PropertyLists 的模板,我的目标是拥有一个带有关联 PropertyLists 和可用 PropertyLists 的表单。在这种形式中,我希望能够进行拖放和排序项目。我正在使用 Rails 5,带有nested_form_fields和JQuery-ui(只有jquery-ui/sortable 模块)。
这是我的模型:
模板:
class Template < ApplicationRecord
has_many :template_property_lists
has_many :property_lists, through: :template_property_lists
accepts_nested_attributes_for :property_lists
validates_presence_of :name
end
Run Code Online (Sandbox Code Playgroud)
属性列表:
class PropertyList < ApplicationRecord
has_many :properties, -> { order(position: :asc) }, dependent: :destroy
has_many :template_property_lists
has_many :templates, through: :template_property_lists
accepts_nested_attributes_for :properties, allow_destroy: true, reject_if: :all_blank
acts_as_list scope: :template, top_of_list: 0
validates_presence_of :name
validates_uniqueness_of :name
end
Run Code Online (Sandbox Code Playgroud)
模板属性列表:
class TemplatePropertyList < ApplicationRecord
belongs_to :template
belongs_to :property_list
end
Run Code Online (Sandbox Code Playgroud)
和 JavaScript …
我有嵌套属性的问题,我有下一个实体:
class Experience < ActiveRecord::Base
has_many :roles, inverse_of: :experience
end
class Role < ActiveRecord::Base
belongs_to :experience, inverse_of: :roles
end
Run Code Online (Sandbox Code Playgroud)
我有下一组参数:
params = {"end_date"=>"02/01/2012",
"city"=>"Buenos Aires",
"state"=>"",
"country"=>"",
"title"=>nil,
"company"=>"Restorando",
"current"=>nil,
"description"=>nil,
"roles_attributes"=>
[{"id"=>558, "title"=>"System Owner", "start_date"=>"03/01/2000", "end_date"=>"02/01/2001", "description"=>"<p>I did a bunch of stuff</p>", "current"=>false},
{"id"=>561, "title"=>"Test", "description"=>nil, "current"=>nil},
{"id"=>557, "title"=>"Full Stack Developerr", "start_date"=>"01/01/2011", "end_date"=>"02/01/2012", "description"=>"<p>Full Stack Developer description</p>", "current"=>false}],
"resumes_experiences_attributes"=>[{"id"=>384, "resume_id"=>809}, {"id"=>385, "resume_id"=>3199}]}
Run Code Online (Sandbox Code Playgroud)
我想更新一个 Rails 模型。所以我执行:
@experience = Experience.find(some_id)
@experience.update_attributes(params)
Run Code Online (Sandbox Code Playgroud)
这给我返回一个错误
ActiveRecord::RecordNotUnique:PG::UniqueViolation:错误:重复的键值违反了唯一约束“roles_pkey”
如果我看到 Rails 试图更新什么,我会看到下一条sql语句:
UPDATE“角色”SET“start_date”= $1,“end_date”= $2,“current”= …
cocoon-gem ×1
fields-for ×1
jquery ×1
jquery-ui ×1
nested-forms ×1
ruby ×1
simple-form ×1
validation ×1