RoR - 嵌套属性错误的参数数量?

时间:2017-12-08 19:20:02

标签: ruby-on-rails ruby ruby-on-rails-5

目前遵循教程,应该实现嵌套表单。但是,当我运行rails c并尝试使用嵌套属性创建一个新的Portfolio时,我得到一个

ArgumentError: wrong number of arguments (given 1, expected 0)
    from app/models/portfolio.rb:4:in `block in <class:Portfolio>'
    from (irb):15

命令我在rails c:

中运行
Portfolio.create!(title: 'Title', subtitle:'Title1', body:'Title3',
technologies_attributes:[{name: 'Ruby'}])

Portfolio.rb文件:

class Portfolio < ApplicationRecord
  has_many :technologies
  accepts_nested_attributes_for :technologies,
                                reject_if: lambda { |attrs| attrs['name'].blank? }


  include Placeholder
  validates_presence_of :title, :body, :main_image, :thumb_image

  def self.angular
    where(subtitle: 'Angular!')
  end

  def self.ruby
    where(subtitle: 'Ruby on Rails!')
  end

  after_initialize :set_defaults

  def set_defaults
    self.main_image ||= Placeholder.image_generator(height: '600', width: '400')
    self.thumb_image ||= Placeholder.image_generator(height: '350', width: '200')
  end

end

任何想法会导致什么?

提前致谢!

2 个答案:

答案 0 :(得分:0)

看起来好像你在这里传递一个哈希数组作为嵌套属性

Portfolio.create!(title: 'Title', subtitle:'Title1', body:'Title3', technologies_attributes:[{name: 'Ruby'}])

尝试传递像这样的属性

params = {
    portfolio: { title: 'Title', subtitle:'Title1', body:'Title3',
        technologies_attributes:[
            {name: 'Ruby'}
        ]
    }
}
Portfolio.create!(params)

答案 1 :(得分:0)

在两个模型中尝试使用inverse_of:因为它曾用于创建关联对象。

recognize.py