在进行嵌套模型表单时,我遇到了这个异常:
ActiveRecord::AssociationTypeMismatch in RecipesController#update
Ingredient(#35624480) expected, got Ingredient(#34767560)
Run Code Online (Sandbox Code Playgroud)
涉及的模型是食谱和成分.配方has_many和accepts_nested_attributes_for:配料,属于:配方.
尝试在配方编辑/更新的嵌套成分表单上_destroy(= 1)预先存在的成分之一时,我得到此异常.
这没什么意义,主要是因为关联类型是预期的(由例外自己承认).
更不经意的是它在功能测试中运行得很好.
此外,如果我重新发布表单(通过更新时的浏览器刷新),它有时也有效.如果我重新启动(开发)网络服务器,它也会工作.
可能导致这种情况的任何想法,或者我应该寻找什么?
我在医院表格中有一个针对部门的嵌套模型.代码段如下所示:
<%= f.simple_fields_for :hospital do |h| %>
.
.
.
<%= h.simple_fields_for :departments do |builder| %>
<%= render "department_fields", :f => builder %>
<% end %>
.
.
<% end %>
Run Code Online (Sandbox Code Playgroud)
_department_fields部分看起来像这样:
<div class="fields">
<%= f.input :name %>
<%= link_to_remove_fields "remove", f %>
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
因此,表单底部有一个位置供用户输入最多三个部门名称.
我正在使用Rails 3,Cucumber,Capybara和Selenium进行集成测试.
在Cucumber中测试此表单时,是否有一种简单的方法可以填写重复字段?
理想情况下,我希望能够像这样编写我的功能:
And I fill in the first "Name" with "Cardiology"
And I fill in the second "Name" with "Radiology"
Run Code Online (Sandbox Code Playgroud)
有没有办法在Cucumber/Capybara中轻松估算出这个?有人已经找到了解决这个问题的一些步骤吗?
我试图通过嵌套模型保存图像
**模型:
Listing
has_many:photos
accepts_nested_attributes_for :photos, :allow_destroy => true
Photo
belongs_to:listing
has_attached_file :data, :styles=>{:featured => "88x63#", :search_result => "122x91#"}
Run Code Online (Sandbox Code Playgroud)
上市控制人:
def new
@listing = Listing.new
@listing.photos.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @listing }
end
end
def create
@listing = Listing.new(params[:listing])
if @listing.save
redirect_to(:action=>'index')
else
render :action => "new"
end
end
Run Code Online (Sandbox Code Playgroud)
视图:
<%= form_for [@listing] do |f| %>
<%= f.fields_for :photos do |ph| %>
<%= ph.file_field :data %>
<% end%>
<%end%>
Run Code Online (Sandbox Code Playgroud)
这里我只提到了视图中的一个字段,但是我使用了很多字段,除了数据(图像)字段外,所有字段都保存在数据库中.
如果我在Photo模型中验证数据的存在,我得到了"照片不应该为空"的消息,尽管我上传了一张图片.
这是我嵌套表单的相关部分:
<div class="field">
<%= f.fields_for "@partcode" do |p|%>
<%= p.label "partcode"%><br />
<%= p.text_field :partcode %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
我已经在我的模型中有这个:
attr_accessible :partcode,
:description
Run Code Online (Sandbox Code Playgroud)
但当我在表单中输入内容时,我收到此错误:
Can't mass-assign protected attributes: @partcode
Run Code Online (Sandbox Code Playgroud)
这是我的零件代码模型:
class Partcode < ActiveRecord::Base
attr_accessible :partcode,
:description
validates :partcode,
:description,
:presence => true
belongs_to "goods_ins"
accepts_nested_attributes_for "goods_ins"
end
Run Code Online (Sandbox Code Playgroud)
这是我的商品在模型中的所有代码:
class GoodsIn < ActiveRecord::Base
attr_accessible :c4lpono,
:courier,
:deliverydate,
:deliverynoteno,
:description,
:destination,
:notes,
:partcode,
:quantity,
:signedby,
:suppliername
validates :deliverydate,
:deliverynoteno,
:destination,
:quantity,
:signedby,
:suppliername,
:presence => true
has_many :suppliers
has_many :partcodes …Run Code Online (Sandbox Code Playgroud) 我的模型中有以下内容:
company.rb
has_many :merits
accepts_nested_attributes_for :merits
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
def new
@company = Company.new
@company.merits.build
end
Run Code Online (Sandbox Code Playgroud)
在我的表格上:
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= f.simple_fields_for :merits do |m|
= m.input :description, :required => false
= m.input :picture, :required => false
Run Code Online (Sandbox Code Playgroud)
这会产生如下哈希:
..."merits_attributes"=>{"0"=>{"description"=>"stove", "picture"=>"www.it.com/stove.png"}}...
我想要的是多个对象如下:
..."merits_attributes"=> {"0"=> {"description"=>"炉灶","图片"=>"www.it.com/stove.png"},"1"=> {"cription "=>"冰箱","图片"=>"www.it.com/fridge.png"}} ...
我可以这样做,我该怎么做呢?如果在控制器中它会创建多个对象使用:
def create
@company = Company.new(params[:company])
if @company.save
sign_in @company
redirect_to root_path
else
render :new
end
Run Code Online (Sandbox Code Playgroud) 因此,我环顾四周,似乎没有人遇到相同的问题,不得不引起这个看似常见的错误。我在我的html中呈现一些表单,如下所示:
<form method="post" action="">
{{ tags_formset.management_form }}
<!-- code displaying this formset -->
...
<!-- -->
<form method="post" action="">
{{ add_all_form.management_form }}
{{ add_all_form.addTagsToAll }}
<input type="submit" value="Add To Displayed Applicants" />
</form>
<form method="post" action="">
{{ remove_all_form.management_form }}
{{ remove_all_form.removeTagsFromAll }}
<input type="submit" value="Remove From Displayed Applicants" />
</form>
<input type="submit" value="Save Changes" />
</form>
Run Code Online (Sandbox Code Playgroud)
当我没有两个内部表单时,将正确显示表单集,并且“提交”按钮可以提交表单。当我添加第二种两种形式时,出现了几个问题:
-提交按钮停止工作(尽管在选择表单集的一个字段时按Enter仍提交表单
-add_all_form的提交工作正常,并且功能正常(不是问题,但有趣的是下一点...)
-remove_all_form无法通过“ ManagementForm数据丢失或已被篡改”验证错误来正常工作。
这是创建表单的views.py代码:
TagsFormSet = formset_factory(TagsForm, formset=TagFormSet, extra=applicantQuery.count())
if request.method == 'POST':
tags_formset = TagsFormSet(request.POST, request.FILES, prefix='tags', applicants=applicantQuery)
add_all_form = TagAddAllForm(request.POST, …Run Code Online (Sandbox Code Playgroud) 我正在遵循Railscast#196修订版,一切都基本上没问题,但我无法在嵌套表单中添加新字段."删除字段"功能运行良好,但点击"link_to_add_fields"后,浏览器会跳转到页面顶部,并且不会显示新字段.
这个轨道广播已经有很多问题了,比如这里或这里,我试着阅读所有这些问题,但大多数都是指2010年的原始演员.我现在被困了几个小时,我不能弄清楚,我的问题在哪里.对不起,如果它是一个新手的错误,我对rails很新.任何帮助将非常感激!
我遵循#196修订版,使用Rails 3.2.13,Ruby 1.9.3
车型/ post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :title, :image, :postfiles_attributes
has_many :postfiles, :dependent => :destroy
accepts_nested_attributes_for :postfiles, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
车型/ postfile.rb
class Postfile < ActiveRecord::Base
attr_accessible :dropbox, :gdrive, :post_id
belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)
意见/职位/ _form.html.erb
<%= simple_form_for @post do |f| %>
<%= f.fields_for :postfiles do |builder| %>
<%= render 'postfile_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add File", f, :postfiles %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
意见/职位/ _postfile_fields.html.erb …
jquery ruby-on-rails nested-forms railscasts ruby-on-rails-3.2
通过关系,我有一个标准的has_many.人类通过连接表交互有许多兽人.互动只是一个表格和模型; 没有控制器或视图.
使用Rails 4中的simpleform gem,我想从人体页面创建一个表单,以便从所有兽人的池中选择多个兽人.提交后,我希望它在交互表中创建/更新尽可能多的记录,每个记录都包含人工ID,并且选择了多个orc ID.:
AKA列表符号
human_id并且orc_id从该列表中选择兽人.(human_id在这些记录中将是相同的,因为它从给定的人类表单页面开始)我将尽可能多地编写整个故事的代码.请随时要求澄清,并解决任何错误,以实现这一点.
表
humans
integer "id"
interactions
integer "human_id"
integer "orc_id"
index ["human_id", "orc_id"]
# This is the primary key. no normal id.
# Is it better to have a primary id for this join table, or does it not matter?
orcs
integer "id"
Run Code Online (Sandbox Code Playgroud)
楷模
/models/human.rb
class Human < ActiveRecord::Base
has_many :interaction
has_many :orcs, through: :interactions
accepts_nested_attributes_for :interactions
end
Run Code Online (Sandbox Code Playgroud)
/models/interaction.rb
# Purely a join model and …Run Code Online (Sandbox Code Playgroud) 我有一个包含许多图像的目录项,并尝试使用嵌套表单和载波通过一个请求上传所有图像。我也使用响应者、haml 和简单的形式。所以,它是这样的:
项目.rb
class Item < ActiveRecord::Base
has_many :images, dependent: :destroy
accepts_nested_attributes_for :images
end
Run Code Online (Sandbox Code Playgroud)
图像.rb
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)
_form.html.haml
= simple_form_for(@item, :html => {:multipart => true }) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.input :description
= f.input :price
= simple_fields_for :images do |image|
= image.file_field :image, multiple: true
.form-actions
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
items_controller.rb
...
def new
@item = Item.new
respond_with(@item)
end
def create
@item = Item.new(item_params)
@item.save
respond_with(@item)
end
... …Run Code Online (Sandbox Code Playgroud) 就我而言,我的客户有很多任务(需要:detail和:completion_date字段。)。我一直在构建如下的嵌套模型形式:
= simple_form_for @client do |f|
= f.simple_fields_for :tasks do |task|
= task.input :detail
= task.input :completion_date
= f.submit
Run Code Online (Sandbox Code Playgroud)
提交表单时,如果没有空白的“详细信息”或“ completion_date”字段,则将重新呈现该表单,但不会显示任何错误消息。
我一直试图寻找解决方案。这些都没有提到对嵌套对象的属性进行验证失败。
希望任何人都能帮忙!谢谢,
ruby-on-rails nested-forms nested-attributes simple-form-for
nested-forms ×10
forms ×2
ruby ×2
simple-form ×2
capybara ×1
carrierwave ×1
cucumber ×1
django ×1
exception ×1
file-upload ×1
html ×1
jquery ×1
paperclip ×1
python ×1
railscasts ×1