Tag: 嵌套属性

Rails – validation嵌套属性唯一性与父级的范围父级

我在Rails中对父级父级的嵌套属性进行作用域唯一性validation时遇到问题。 背景 我有一个带有3个型号的rails 4应用程序: #app/models/account.rb class Account < ActiveRecord::Base has_many :contacts, dependent: :destroy end #app/models/contact.rb class Contact < ActiveRecord::Base belongs_to :account has_many :email_addresses, dependent: :destroy, validate: :true, inverse_of: :contact accepts_nested_attributes_for :email_addresses,allow_destroy: true validates :email_addresses, presence: true end #app/models/email_address.rb class EmailAddress < ActiveRecord::Base belongs_to :contact, inverse_of: :email_addresses validates :label, presence: true validates :contact, presence: true validates […]

Rails 4删除嵌套属性,适用于创建但不适用于编辑/更新

所以我正在使用这个Railscast 。 而且我知道Rails 4中的强参数有一些变化。 第一个相关问题 。 第二个相关问题 我已经四倍检查了我的实现,但看不出我出错的地方。 就目前而言,在最初提交患者时(即创建方法)勾选“销毁”框会按预期工作,并将删除任何带有复选框的药物,并允许任何不具有此function的药物(来自三种forms的输入)它提供)。 然而,当我随后编辑该患者时,任何未被检查被删除的药物都是重复的(因此我最终得到的药物比我开始时更多),并且任何检查删除的药物似乎都没有改变。 因此,如果有两种药物附加“Med1”和“Med2”,并且我编辑患者,如果两者都被标记为删除,我仍然会以“Med1”和“Med2”结束。 如果只有“Med1”标记为删除,我将以“Med1”和“Med2”以及额外的“Med2”结束。 如果两者都没有标记为删除,我将最终得到两个“Med1”和“Med2”。 #patient.rb class Patient true, :reject_if => lambda { |a| a[:name].blank? }, end #views/patients/_form.html.erb prohibited this patient from being saved: builder %> #views/patients/medications_fields.html #controllers/patients_controller.rb class PatientsController < ApplicationController before_action :set_patient, only: [:show, :edit, :update, :destroy] # GET /patients # GET /patients.json def index […]

未允许的参数嵌套属性 – rails

我正在尝试将表单提交到2个表,但不知怎的,我在这一行joins: [‘sources’], :landslide_id了这个语法错误unexpected ‘\n’ joins: [‘sources’], :landslide_id found unpermitted parameter: sources joins: [‘sources’], :landslide_id并found unpermitted parameter: sources滑坡参数的found unpermitted parameter: sources 。 这是所有文件 楷模 class Landslide < ApplicationRecord has_many :sources, dependent: :destroy accepts_nested_attributes_for :sources class Source < ApplicationRecord belongs_to :landslide end landslides_controller.rb def new @landslide = Landslide.new @landslide.sources.build end # POST /landslides def create @landslide = […]

为嵌套属性添加删除链接

我在show.erb中嵌套了属性,并创建了一个空白的嵌套属性,并显示了底部空白的项目网格,如此。 我想要一个link_to’Destroy’工作,但当我将其添加到网格时,我得到undefined method ‘plural’ “answer”, :confirm => ‘Are you sure?’, :method => :delete %>

嵌套表单更新操作产生重复结果

在嵌套表单的更新操作期间,它似乎创建了新的嵌套记录,而不是更新当前嵌套的记录。 这就是我的控制器的样子: class ApplicationsController “You have now applied!” end def edit @application = Application.find(params[:id]) @answers = [] @job.questions.each do |question| @application.answers.each do |answer| @answers < “You have updated your application!” end def destroy Application.find(params[:id]).destroy flash[:success] = “Application Deleted.” redirect_to root_url end def show @application = Application.find(params[:id]) @answers = [] @job.questions.each do |question| @application.answers.each do |answer| […]

Rails 4不通过JSON更新嵌套属性

我已经搜索了相关的问题,但仍然有问题从我的AngularJS前端返回的rails 4到JSON中更新嵌套属性。 问题:下面的代码概述了从我的Rails4应用程序中从AngularJS传递到Candidate模型的JSON。 Candidate模型有很多Works,我正在尝试通过Candidate模型更新Works模型。 由于某种原因,Works模型无法更新,我希望有人可以指出我缺少的东西。 谢谢你的帮助。 这是候选人AngularJS前端的json: {“id”=>”13”, “nickname”=>”New Candidate”, “works_attributes”=>[ {“title”=>”Financial Analyst”, “description”=>”I did things”}, {“title”=>”Accountant”, “description”=>”I did more things”}]} 然后,Rails通过添加候选标头将此JSON转换为以下内容,但不包括候选标头下的嵌套属性,并且无法通过候选模型更新works_attributes : {“id”=>”13”, “nickname”=>”New Candidate”, “works_attributes”=>[ {“title”=>”Financial Analyst”, “description”=>”I did things”}, {“title”=>”Accountant”, “description”=>”I did more things”}], “candidate”=>{“id”=>”13”, “nickname”=>”New Candidate”}} candidate_controller.rb包含一个简单的更新: class CandidatesController < ApplicationController before_filter :authenticate_user! respond_to :json def update respond_with Candidate.update(params[:id], candidate_params) end […]

Rails嵌套属性表单用于多态/单表inheritance关联

我正在使用一个表单(使用SimpleForm),允许您编辑嵌入的关联。 我遇到的问题是嵌套模型是子类,因此它们是具有可能不同字段的不同类型。 我正在为每种类型的模型创建隐藏表单,并使用JavaScript显示所选类型的表单。 仅供参考,我使用以下gem: Rails 3.2 Mongoid 简单的forms 这是我到目前为止的简化示例: class Garage include Mongoid::Document embeds_one :vehicle accepts_nested_attributes_for :vehicle end class Vehicle include Mongoid::Document embedded_in :garage attr_accessible :_type end class Car < Vehicle field :car_field attr_accessible :car_field end class Truck < Vehicle field :truck_field attr_accessible :truck_field end 在控制台中: > garage = Garage.new > garage.vehicle = Car.new(car_field: ‘something’) […]

accepts_nested_attributes_for:我做错了什么

我尝试在rails4中创建一对多连接。 但是,虽然我没有收到错误,但不存储嵌套属性。 我究竟做错了什么? 站的模型 class Station < ActiveRecord::Base has_many :adresses accepts_nested_attributes_for :adresses end ADRESS-型号 class Adress < ActiveRecord::Base belongs_to :station end Station-Controller类StationsController <ApplicationController def new @station = Station.new @station.adresses.build end def create @station = Station.new(station_params) @station.save redirect_to @station end def index @stations = Station.all end private def station_params params.require(:station).permit(:name, adresses_attributes: [ :url ]) end end […]

嵌套属性可以与inheritance结合使用吗?

我有以下课程: 项目 人 人 > 开发人员 人 > 经理 在Project模型中,我添加了以下语句: has_and_belongs_to_many :people accepts_nested_attributes_for :people 当然还有Person类中的相应语句。 如何通过nested_attributes方法将Developer添加到Project ? 以下不起作用: @p.people_attributes = [{:name => “Epic Beard Man”, :type => “Developer”}] @p.people => [#] 如您所见, type属性设置为nil而不是”Developer” 。

rspec +工厂模型测试嵌套属性

我有一个带有rspec + factory_girl的rails4应用程序。 我想测试validation,确保产品至少有一个function,竞争,用例和行业。 前3个必须属于产品,但行业可以独立存在。 我还没有试过把工业放在测试中,因为我甚至不能把工作放在前3。 我尝试了下面的方法,在其中我创建了一个具有product_feature,product_competition和product_usecase的产品工厂。 由于某种原因,它不起作用。 我使用正确的方法吗? 如果是这样,我的代码出了什么问题? 1) Product nested attribute validation has a valid factory Failure/Error: expect(create(:product_with_nested_attrs)).to be_valid ActiveRecord::RecordInvalid: Validation failed: You have to choose at least 1 industry., You must have at least 1 product feature., You must name at least 1 competition., You must describe at least 1 usecase. […]