Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用一个表单更新多个记录_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby 使用一个表单更新多个记录

Ruby 使用一个表单更新多个记录,ruby,ruby-on-rails-3,activerecord,Ruby,Ruby On Rails 3,Activerecord,我有以下情况:用户有多个资产,每个资产都有一个资产详细信息记录 型号: class User < ActiveRecord::Base has_many :assets end class Asset < ActiveRecord::Base has_one :asset_detail belongs_to :user accepts_nested_attributes_for :asset_detail, :allow_destroy => false

我有以下情况:
用户
有多个
资产
,每个
资产
都有一个
资产详细信息
记录

型号:

class User < ActiveRecord::Base
  has_many :assets
end

class Asset < ActiveRecord::Base
  has_one :asset_detail
  belongs_to :user

  accepts_nested_attributes_for :asset_detail,
  :allow_destroy => false

  attr_accessible # ...
end

class AssetDetail < ActiveRecord::Base
  belongs_to :asset

  attr_accessible # ...
end
def edit
  @user   = current_user
  @assets = Asset.all
end

def update
  @user = current_user
  @user.update_attributes(params["user"])
end
= form_for @user, url: 'update action url' do |f|
  = f.fields_for :assets do |ff|
    = ff.text_field :title 
    = ff.fields_for :asset_detail do |fff|
      = fff.text_field :value
查看:

class User < ActiveRecord::Base
  has_many :assets
end

class Asset < ActiveRecord::Base
  has_one :asset_detail
  belongs_to :user

  accepts_nested_attributes_for :asset_detail,
  :allow_destroy => false

  attr_accessible # ...
end

class AssetDetail < ActiveRecord::Base
  belongs_to :asset

  attr_accessible # ...
end
def edit
  @user   = current_user
  @assets = Asset.all
end

def update
  @user = current_user
  @user.update_attributes(params["user"])
end
= form_for @user, url: 'update action url' do |f|
  = f.fields_for :assets do |ff|
    = ff.text_field :title 
    = ff.fields_for :asset_detail do |fff|
      = fff.text_field :value

问题是所有表单字段都已正确填充,但我无法保存它们。表单发送时没有任何错误,但数据没有更新。

我认为您的模型应该如下所示:

class User < ActiveRecord::Base
  attr_accessible :assets_attributes #...
  has_many :assets
  accepts_nested_attributes_for :assets_attributes
end

class Asset < ActiveRecord::Base
  attr_accessible :asset_detail_attrbutes # ...
  has_one :asset_detail
  belongs_to :user

  accepts_nested_attributes_for :asset_detail_attributes,
  :allow_destroy => false
end
class用户false
结束

原因是,您需要能够通过传递给每个模型的属性散列来设置属性。嗯

您应该检查
@user.update_attributes(params[“user]”)的返回值。
…只是好奇-资产/资产详细信息结构的用途是什么?您是否可以将资产详细信息属性滚动到资产中?好的,我提供的代码只是我正在编码的系统的简化版本,它确实需要这样;)基本上,资产表有多种类型的资产。其中一个是
business
,只是其中一个与资产详细信息(实际上称为
business\u details
)有关。表单中是否保存了任何内容?是否保存用户信息或资产信息?您说过update_属性返回true,所以似乎有什么东西可以保存。另外:你可以看看params[:user]散列,然后在这里发布一个经过消毒的样本吗?