Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 on rails 4 rails 4嵌套属性不会创建多个模型_Ruby On Rails 4_Radio Button_One To Many_Nested Attributes_Fields For - Fatal编程技术网

Ruby on rails 4 rails 4嵌套属性不会创建多个模型

Ruby on rails 4 rails 4嵌套属性不会创建多个模型,ruby-on-rails-4,radio-button,one-to-many,nested-attributes,fields-for,Ruby On Rails 4,Radio Button,One To Many,Nested Attributes,Fields For,我是rails新手,在这方面花了太多时间。事先非常感谢您的帮助 我似乎无法获取字段和/或接受嵌套属性来处理嵌套属性。 我有一个smash_客户机,它有很多合约,还有一个表单,它试图创建一个带有参数的smash_客户机,同时它还试图在合约对象上设置一个参数。该合同属于smash客户。 我已经尝试了很多不同的解决方案,并且已经阅读了文档,但是我仍然缺少一些东西。我在我的params散列中,在smash_clients_controller.rb中得到这个 ..., "smash_client"=&g

我是rails新手,在这方面花了太多时间。事先非常感谢您的帮助

我似乎无法获取字段和/或接受嵌套属性来处理嵌套属性。 我有一个smash_客户机,它有很多合约,还有一个表单,它试图创建一个带有参数的smash_客户机,同时它还试图在合约对象上设置一个参数。该合同属于smash客户。 我已经尝试了很多不同的解决方案,并且已经阅读了文档,但是我仍然缺少一些东西。我在我的params散列中,在smash_clients_controller.rb中得到这个

..., "smash_client"=>{"name"=>"fasdf", "user"=>"adam"}, "smash_client_id"=>{"instance_type"=>"spot"},...

我试着使用:contracts、@smash\u client.contracts和部分字段中的其他内容。我也尝试过使用select和collection\u select,但我似乎无法确定表单。 抱歉发了这么长的邮件。希望我得到了所有有用的信息,没有额外的问题。 我真的很想知道一些方向或答案。
提前感谢。

而不是:字段_for:smash _client _iddo | c|

写为:字段_for:contracts do | c|

参考: 1.

-有关在控制器中编写代码的信息,请参阅本手册,并查看正确的方法

我终于找到了。必须在合同模型中将:instance_类型列入白名单。再次感谢你,卡里亚尼。我感谢你的帮助。以下是对上述代码的更改:

.field
  = fields_for :contracts do |c|
    = c.label :instance_type, 'spot instance'
    = c.radio_button :instance_type, 'spot', checked: true
    = c.label :instance_type, 'on demand instance'
    = c.radio_button :instance_type, 'on_demand'


你能给我指一下链接中我应该读的部分吗?我在发布我的问题之前尝试了这个答案,但没有结果。此外,我还尝试在部分的字段_中使用:contracts,但这不会执行嵌套参数。它在params散列中也不会显示为:contracts\u属性,而是显示为:contracts。我已经非常彻底地阅读了这些文档,但是我对rails非常陌生,所以我认为我仍然缺少一些东西。顺便说一下,谢谢!我想这可能有助于澄清问题。这是我试图从控制台创建一个。smash_client_params=>{:name=>foo,:user=>adam,:contracts_attributes=>[{:instance_type=>spot}]}smash client.create smash_client_params=>插入smash_clients`name,user,created_at,updated_at值'foo',adam',created at/updated at'INSERT in contracts实例类型,smash_client\u id,created_at,updated_at值'spot',5,“创建时间/更新时间”`=>回滚SystemStackError:堆栈级别太深
class SmashClient < ActiveRecord::Base
  has_many :contracts, dependent: :destroy
  accepts_nested_attributes_for :contracts, allow_destroy: true, 
    reject_if: proc { |attributes| attributes[:instance_type].blank? }
...  
  def new
    @smash_client = SmashClient.new
    3.times { @smash_client.contracts.build }
  end
...
  def smash_client_params
    @smash_client_params = params.require(:smash_client).
      permit( :user, :name, contracts_attributes: [:instance_type] )
  end
end
class Contract < ActiveRecord::Base
  belongs_to :smash_client
  after_create :determine_instance_type_and_start
  before_destroy :stop_instances
...
end
smash_client_params = {name: 'something', user: 'blah', contracts_attributes: [{instance_type: 'spot'}]}
SmashClient.create( smash_client_params )
.field
  = fields_for :contracts do |c|
    = c.label :instance_type, 'spot instance'
    = c.radio_button :instance_type, 'spot', checked: true
    = c.label :instance_type, 'on demand instance'
    = c.radio_button :instance_type, 'on_demand'
def contract_params
  params.require(:contract).
    permit(:id, :name, :instance_id, :smash_client_id, :instance_type)
end