给定模型Albumhas_many Song,后者使用本地化字段,例如:
Song#name_en
Song#description_en
Song#name_fr
Song#description_fr
[...]
Run Code Online (Sandbox Code Playgroud)
由于前端设计,我无法f.simple_fields_for :songs在一个地方为所有歌曲属性做一个,但需要拆分它:
= f.simple_fields_for :songs do
= render partial: 'song_en_fields', locals: { f: f, locale: :en }
[...]
= f.simple_fields_for :songs do
= render partial: 'song_fields', locals: { f: f, locale: :fr }
[...]
Run Code Online (Sandbox Code Playgroud)
得到的字段建立索引用[0],[1]等他们应该,但是,指数不为0的每个indvidivual重新启动simple_fields_for,但只是保持计数.
我检查了源代码并index在Rails中找到了一个选项fields_for,但这只是添加了一个额外的索引数组.
有没有办法在同一集合多次调用simple_fields_for(或fields_for)时"重置"索引的自动增量?
我有动态表单,它有一组值.我创建了一个包含我显示的文本字段的部分.在每个人旁边,我想显示一个包含文本标题的标签.例如,先前不知道名字和姓氏.我该怎么做呢?似乎我无法直接访问属性.但是当我使用label字段时,标签中的变量名称不会显示实际值.
嗨,我有任何形式的嵌套形式,例如
<% form_for :main do |f| %>
trying to insert code here
<% fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后我试图将任何东西插入主窗体,嵌套窗体不会产生任何输出.它只在它是主窗体中唯一的对象时输出.有什么建议?
我之前发布了一个关于此的问题,并被建议阅读大量相关信息.我已阅读并尝试实施约30种不同的解决方案.这些都不适合我.
这就是我所拥有的.
我有一个Miniatures模型.我有制造商型号.Miniatures有许多制造商通过Productions模型.
这些关联似乎设置正确,因为我可以在我的视图中显示它们并通过控制台创建它们.我遇到问题的方法是让Miniatures NEW和EDIT视图创建并更新到Productions表.
在控制台中的命令 @miniature.productions.create(manufacturer_id: 1)有效,这使我相信我应该能够在表单中执行相同的操作.
我认为我的问题总是在Miniatures Controller中,特别是CREATE函数.我在那里尝试了很多其他人的解决方案,没有人能做到这一点.我的field_for形式的东西也可能是错误的,但这似乎不那么繁琐.
我已经坚持了几天,虽然还有其他我可以继续工作的事情,如果这种关联不可能,那么我需要重新考虑我的整个应用程序.
表单现在在Productions表中创建一行,但不包括所有重要的manufacturer_id.
任何帮助非常感谢.
我的新微缩形式
<% provide(:title, 'Add miniature') %>
<h1>Add a miniature</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@miniature) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :production do |production_fields| %>
<%= production_fields.label :manufacturer_id, "Manufacturer" %>
<%= production_fields.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name) %>
<% end %>
<%= f.label :release_date %>
<%= f.date_select :release_date, :start_year => Date.current.year, :end_year => …Run Code Online (Sandbox Code Playgroud) 这里有很多很好的问题和答案,关于获取nested_form,collection_select,accepts_nested_attributes_for和fields_for可以很好地一起玩的互联网,但我仍然难过.如果你能帮助我,请提前致谢.
目标:生成新的isbn记录.isbn可以有很多贡献者.我成功地使用ryanb nested_form gem根据需要在表单上动态生成新的贡献者字段.其中一个字段使用Contributor中的所有名称记录的collection_select下拉列表.创建新记录时,需要将许多贡献者ID写入连接表(contributors_isbns).
我有一些工作,但只是我可以将一个贡献者ID保存到isbns表中的新记录.我似乎无法将任何数据写入连接表.
我有三个型号.贡献者和Isbns有很多关系,我用has_many做过:通过.isbn可以拥有许多贡献者,贡献者可以拥有许多isbns.他们通过contributors_isbn加入.
isbn.rb
attr_accessible :contributor_id
has_many :contributors, :through => :contributors_isbns
has_many :contributors_isbns
accepts_nested_attributes_for :contributors
accepts_nested_attributes_for :contributors_isbns
Run Code Online (Sandbox Code Playgroud)
contributor.rb
attr_accessible :isbn_id
has_many :contributors_isbns
has_many :isbns, :through => :contributors_isbns
accepts_nested_attributes_for :isbns
Run Code Online (Sandbox Code Playgroud)
contributors_isbn.rb
class ContributorsIsbn
attr_accessible :isbn_id, :contributor_id
belongs_to :isbn
belongs_to :contributor
accepts_nested_attributes_for :contributors
Run Code Online (Sandbox Code Playgroud)
在isbns控制器中:
def new
@isbn = Isbn.new
@title = "Create new ISBN"
1.times {@isbn.contributors.build}
@isbn.contributors_isbns.build.build_contributor
end
Run Code Online (Sandbox Code Playgroud)
(显然我无法理解使用哪种构建方法.)
在isbns new.html.erb视图中:
<%= nested_form_for @isbn, :validate => false do |f| %>
<h1>Create new ISBN</h1>
<%= render …Run Code Online (Sandbox Code Playgroud) 我有一个场地表,我在场地编辑页面上使用嵌套表格为每个场地添加优惠.但是,每次添加新商品时,fields_for表单都会保存输入的文本,并为要添加的其他商品记录创建新的空白表单.
我只想为每个添加的记录添加一个"添加新优惠"表单.
没有添加任何优惠 - 这很好:

添加了一个优惠 - 现在theres 2'添加新优惠'表单和不需要的空白优惠部分:

添加一个优惠后,这就是我想要的样子:

新的空白报价表格和空白部分的数量随着控制器中内置的数字的变化而变化(在1分钟时)
场馆管制员
class VenuesController < ApplicationController
def edit
@venue = Venue.find(params[:id])
1.times { @venue.offers.build }
end
def update
@venue = Venue.find(params[:id])
if @venue.update_attributes(params[:venue])
flash[:notice] = 'Venue updated successfully'
redirect_to :back
end
end
end
Run Code Online (Sandbox Code Playgroud)
场地模型
class Venue < ActiveRecord::Base
has_many :offers
accepts_nested_attributes_for :offers
end
Run Code Online (Sandbox Code Playgroud)
场地edit.html.erb
<%= form_for @venue do |f| %>
<h2 class="venue_show_orange">Offers</h2>
<div class="edit_venue_details">
<% if @venue.offers.count.zero? %>
<div class="no_offers">
No offers added yet.
</div>
<% else %>
<%= render …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个表单,允许我为关联输入分组的关联提交新记录.
class Product < AR::Base
has_many :properties
accepts_nested_attributes_for :properties
end
Run Code Online (Sandbox Code Playgroud)
请注意,在控制器中,为产品构建了一系列属性,因此@product.properties.empty? # => false.
下面fields_for给出了正确的输入,例如product[properties_attributes][0][value].
= form.fields_for :properties do |pform|
= pform.input :value
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试对关联进行分组,它就不再生成具有正确名称的输入:
- @product.properties.group_by(&:group_name).each do |group_name, properties|
%h3= group_name
= form.fields_for properties do |pform|
= pform.input :value
Run Code Online (Sandbox Code Playgroud)
这创建了name属性的输入,product[product_property][value]实际上它应该是product[property_attributes][0][value]第一个例子.
Rails文档建议您可以这样做:
= form.fields_for :properties_attributes, properties do |pform|
Run Code Online (Sandbox Code Playgroud)
但这会给出错误"Array的未定义方法值".
目前,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
我有一个嵌套的表单(订单中的付款),我想在编辑视图中测试我的嵌套表单(fields_for)中的值.但问题是我无法检查每个,我可以这样做:
<% if @order.payments[0].monthly == false %>
Run Code Online (Sandbox Code Playgroud)
你现在是如何检查每个,如:
<% if @order.payments[current_payment].monthly == false %>
Run Code Online (Sandbox Code Playgroud) 我正在创建简单的博客级应用程序.以下是我的模特.
class User < ActiveRecord::Base
attr_accessible :name,:posts_count,:posts_attributes , :comments_attributes
has_many :posts
has_many :comments
accepts_nested_attributes_for :posts , :reject_if => proc{|post| post['name'].blank?} , :allow_destroy => true
end
class Post < ActiveRecord::Base
attr_accessible :name, :user_id ,:comments_attributes
belongs_to :user
has_many :comments
accepts_nested_attributes_for :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content, :post_id, :user_id
belongs_to :user
belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)
我试图通过使用accepts_nested_attributes_forrails的功能在一个表单中创建用户,发布和评论.下面是我的控制器和视图代码.
控制器-----------
class UsersController < ApplicationController
def new
@user = User.new
@post = @user.posts.build
@post.comments.build
end
def create
@user = User.new(params[:user])
@user.save
end
end
Run Code Online (Sandbox Code Playgroud)
形成 …