【问题标题】:rails 4 nested attributes won't create has_many modelrails 4嵌套属性不会创建has_many模型
【发布时间】:2015-04-13 06:42:34
【问题描述】:

我是 Rails 新手,在这方面花了太多时间。非常感谢,提前,任何帮助!

我似乎无法让 fields_for 和/或 accept_nested_attributes_for 为我的嵌套属性工作。 我有一个 has_many 合同的smash_client 和一个尝试创建带有参数的smash_client 的表单,同时它还尝试在合同对象上设置一个参数。合约belongs_tosmash_client。 我尝试了很多不同的解决方案并阅读了文档,但我仍然缺少一些东西。我在我的 params 哈希中得到了这个,在smash_clients_controller.rb

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

来自

= form_for @smash_client do |f|
  .field
    = f.label :name
    = f.text_field :name
  .field
    = fields_for :smash_client_id do |c|
      %p
        = c.radio_button :instance_type, 'spot'
        = c.label :instance_type, 'spot'
        = c.radio_button :instance_type, 'on_demand'
        = c.label :instance_type, 'on demand'

  .actions
    = f.submit 'Save'

和

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

我认为如果我硬编码嵌套的参数会起作用,因为如果我尝试这样的事情,在控制台中,我不会收到错误,我会得到一个新的 SmashClient 和 Contract。

smash_client_params = {name: 'something', user: 'blah', contracts_attributes: [{instance_type: 'spot'}]}
SmashClient.create( smash_client_params )

我尝试在fields_for 部分使用:contracts、@smash_client.contracts 和其他一些东西。也尝试使用 select 和 collection_select 但我似乎无法确定表格。 对不起,很长的帖子。希望我得到了所有有用的信息,而问题中没有任何额外内容。 我真的很感激一些方向或答案。 提前致谢。

【问题讨论】:

    标签: ruby-on-rails-4 radio-button one-to-many nested-attributes fields-for


    【解决方案1】:

    我终于找到了。 :instance_type 必须在 Contract 模型中列入白名单。再次感谢,卡利亚尼。我很感激帮助。以下是对上述代码的更改:

    .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
    

    【讨论】:

      【解决方案2】:

      而不是:fields_for :smash_client_id do |c|

      写成:fields_for :contracts do |c|

      参考: 1.http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

      1. http://railscasts.com/episodes/196-nested-model-form-part-1

      2. Rails 4 Nested Attributes Unpermitted Parameters ---- 参考这里在控制器中编写代码并查看正确方法

      【讨论】:

      • 您能指出我应该阅读的链接中的部分吗?在发布我的问题之前,我尝试了该答案,但无济于事。另外,我尝试在 fields_for 部分中使用 :contracts ,但这并没有执行嵌套参数。它也不会在参数哈希中显示为:contracts_attributes,而是以:contracts 出现。我已经非常彻底地阅读了这些文档,但我对 Rails 很陌生,所以我想我仍然缺少一些东西。谢谢,顺便说一句!
      • 我认为这可能有助于澄清问题。这是我尝试从控制台创建一个。 smash_client_params =&gt; {:name=&gt;"foo", :user=&gt;"adam", :contracts_attributes=&gt;[{:instance_type=&gt;"spot"}]} SmashClient.create smash_client_params =&gt; INSERT INTO smash_clients` (name, user, created_at, updated_at) 值 ('foo', 'adam', '创建时间/更新时间') INSERT INTO contracts (instance_type, smash_client_id, created_at, updated_at) VALUES ('spot', 5, 'created at/updated at time')`=&gt; ROLLBACK SystemStackError: stack level too deep
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多