Tag: 嵌套表单

为什么在创建模型对象时会获得AssociationTypeMismatch?

我收到以下错误: ActiveRecord::AssociationTypeMismatch in ContractsController#create ExchangeRate(#2183081860) expected, got HashWithIndifferentAccess(#2159586480) Params: {“commit”=>”Create”, “authenticity_token”=>”g2/Vm2pTcDGk6uRas+aTgpiQiGDY8lsc3UoL8iE+7+E=”, “contract”=>{“side”=>”BUY”, “currency_id”=>”488525179”, “amount”=>”1000”, “user_id”=>”633107804”, “exchange_rate”=>{“rate”=>”1.7”}}} 我的相关模型是: class Contract < ActiveRecord::Base belongs_to :currency belongs_to :user has_one :exchange_rate has_many :trades accepts_nested_attributes_for :exchange_rate end class ExchangeRate “Currency” belongs_to :numccy, :class_name=>”Currency” belongs_to :contract end 我的观点是: Username: B/S: Currency: Amount: Rate: 我的视图控制器: class ContractsController @contract } end end def […]

使用accepts_nested_attributes_for创建新记录或更新现有记录

阅读最新信息以获取最新信息。 嘿大家, 我在rails应用程序中有多对多的关系,涉及三个表:用户表,兴趣表和加入user_interests表,它还具有评级值,因此用户可以对其中的每个兴趣进行评级。 1-10比例。 我基本上在寻找一种新用户在未来的日期注册和编辑它们以及同时编辑任何个人资料信息时创建评级的方法。 我试着用has_many来跟踪这个问题Rails嵌套表单:通过,如何编辑连接模型的属性? 但我遇到的问题是试图将选择列表合并到混合中并具有多个兴趣来为用户评分。 型号代码: user.rb has_many :user_interests, :dependent => :destroy has_many :interests, :through => :user_interests, :foreign_key => :user_id accepts_nested_attributes_for :user_interests interest.rb has_many :user_interests, :dependent => :destroy has_many :users, :through => :user_interests, :foreign_key => :interest_id, :dependent => :destroy user_interest.rb belongs_to :user belongs_to :interest 查看代码: app/views/user/_form.html.erb … user fields … loop through ALL […]

Rails:validationhas_many关联中parent_id的存在

我有一个项目资源,有很多任务。 我想通过将validates_presence_of :project_id添加到任务模型来确保每个任务都有一个project_id 。 但是,在使用任务创建新项目时,在记录保存之前project_id将不可用,因此我无法使用validates_presence_of :project_id 。 所以我的问题是,如何在任务模型中validationproject_id的存在? 我想确保每个任务都有父母。 … class Project :destroy accepts_nested_attributes_for :tasks, :allow_destroy => true … class Task < ActiveRecord::Base belongs_to :project validates_presence_of :project_id

many-to-many:has_many:通过关联表单与分配给链接模型的数据创建表单视图

我正在玩Rails指南中的一个例子: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association 此示例具有以下模型设置: class Physician :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient :appointments end 我试图了解如何做以下两件事: 如何设置创建新患者的视图,并为具有指定时间的现有医生分配约会 如何为现有患者分配新医师和预约时间 我经历了处理嵌套表单的RailsCasts 196和197,但我不知道它将如何应用于这种情况。 有人可以提供一个例子或指向我这方面的指南吗? 谢谢

fields_for not rendering – rails 3

最后转移到Rails 3进行一个新项目并且已经遇到了一个菜鸟问题。 试图做一个简单的嵌套表单。 2个型号:列表和任务 列表模型 class List :destroy accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? } end 任务模型 class Task < ActiveRecord::Base belongs_to :list end 列表控制器 def new @list = List.new 3.times{ @list.tasks.build } end 列表/ new.html.erb {:action=>”create”} do |f| %> ‘big’ %> “submit”, :name => “submit”, :class => “form_submit”, :disabled => false, :disable_with […]

嵌套表单触发’无法批量分配受保护属性警告

我有一个多层嵌套表单 用户 – >任务 – >前提条件 并以相同的forms 用户 – >任务 – >位置 位置表单工作正常,现在我正在尝试指定当前任务的先决条件。 先决条件是存储在:completed_task字段中的task_id。 当我提交表单时,我在输出中收到以下错误 警告:无法批量分配受保护的属性:prerequisite_attributes 对用户中的每个任务发出一个警告。 我已经完成了与此相关的所有其他问题,确保正确引用字段名称:completed_task, 将attr_accessible添加到我的模型中(它已经存在并且我扩展了它)。 我不确定我应该做什么。 我的模特看起来像 class Task <ActiveRecord :: Base attr_accessible:user_id,:date,:description,:location_id belongs_to:用户 has_one:location accepts_nested_attributes_for:location has_many:先决条件 accepts_nested_attributes_for:先决条件 结束 class先决条件<ActiveRecord :: Base attr_accessible:completed_task belongs_to:任务 结束 表格使用formtastic,我包括表格via builder3%> — _prerequisite_fields.html.erb —– 有什么建议?

带有多个图像的嵌套回形针forms

我在Banana模型和Image模型之间有一对多的关联。 此外,每个香蕉和图像属于一个用户(通过单独的关联,因为图像及其香蕉可能有不同的用户)。 我想要一个嵌套的表单来创建香蕉和图像。 踢球者是我不知道要构建多少个图像(注意多个属性)。 下面表单中注释掉的位将创建适当数量的图像,但不会完成关联的用户参考。 有没有办法用fields_for来完成这个(所以关联完成)就像我试过的那样? 香蕉模型 class Banana < ActiveRecord::Base belongs_to :user validates_presence_of :user has_many :images, dependent: :destroy accepts_nested_attributes_for :images validates_associated :images end 图像模型 class Image < ActiveRecord::Base belongs_to :user validates_presence_of :user belongs_to :banana validates_presence_of :banana has_attached_file :img end 形成 true, :html => { :multipart => true } do |f| %> <!– –> CONTROLLER […]

Rails has_many:通过嵌套表单

我刚刚跳进了has_many :through联想。 我正在尝试通过单一表单实现为所有3个表( Physician , Patient和关联表)保存数据的function。 我的迁移: class CreatePhysicians < ActiveRecord::Migration def self.up create_table :physicians do |t| t.string :name t.timestamps end end end class CreatePatients < ActiveRecord::Migration def self.up create_table :patients do |t| t.string :name t.timestamps end end end class CreateAppointments < ActiveRecord::Migration def self.up create_table :appointments do |t| t.integer :physician_id t.integer :patient_id t.date :appointment_date […]

Rails 3.1+嵌套表单问题:无法批量分配受保护的属性

我有一个篮球应用程序,其中一个名单有很多玩家,一个玩家可以在多个名单上。(多对多的原因是玩家 – 名册档案) Roster.rb class Roster :rosterizes accepts_nested_attributes_for :players attr_accessible :jersey_number, :team_id, :class_year, :players end Rosterizes.rb (名字不好我知道……) class Rosterize < ActiveRecord::Base belongs_to :player belongs_to :roster attr_accessible :player_id, :roster_id end Player.rb class Player :rosterizes validates_presence_of :first_name, :last_name attr_accessible :first_name, :last_name, :active end 风景 New Player player_added_team_path, :html => { :class => ‘form-horizontal’ } do |f| %> […]

accepts_nested_attributes_for链接到现有记录,而不是创建新记录

我有以下型号 class Order < AR::Base has_many :products accepts_nested_attributes_for :products end class Product < AR::Base belongs_to :order has_and_belongs_to_many :stores accepts_nested_attributes_for :stores end class Store < AR::Base has_and_belongs_to_many :products end 现在我有一个订单视图,我想更新产品的商店。 问题是我只想将产品连接到我的数据库中的现有商店,而不是创建新商店。 我在订单视图中的表单看起来像这样(使用Formtastic): = semantic_form_for @order do |f| = f.inputs :for => :live_products do |live_products_form| = live_products_form.inputs :for => :stores do |stores_form| = stores_form.input :name, :as => […]