0

I'm trying to use accepts_nested_attributes_for as described at http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

I assumed that second block of code in the tutorial is meant to be in the model, since he says later to do nothing to the controller. The scope seems to signal controller code, though. I have added the following code to the model for "scan", which is supposed to generate child "hostScan" objects before creation of a scan

class Scan < ActiveRecord::Base
  attr_accessible :description, :endTime, :startTime, :raw
  has_many :hostScans, dependent: :destroy
  accepts_nested_attributes_for :hostScans, :allow_destroy => true

  before_create :interpret

  def interpret

    #parse the start and end times of the scan
self.startTime = raw.split(/(?<=timestamps\|\|\|scan_start\|)(.*?)(?=\|)/)[1]
self.endTime = raw.split(/(?<=timestamps\|\|\|scan_end\|)(.*?)(?=\|)/)[1]

#host scan bodies
    #host name
        #hostScans = raw.scan(/(?<=timestamps\|\|)(.*?)(?=\|host_start)/)
        #self.HostScans_attributes = [{}]
    #raw host text
        hostScanBodies = raw.split(/(?<=host_start\|)(.*?)(?=host_end)/)

        hostScanBodies.each do |body|
            self.HostScans_attributes += [{:raw => body}]
        end
  end
end

However, when I try to create a scan, I get the following error:

NoMethodError in ScansController#create

undefined method `HostScans_attributes' for #<Scan:0x2441e68>

It doesn't seem to know about HostScans_attributes.

2 Answers 2

1

Firstly, try using under_score notation rather than camelCase - rails expects that by convention. When using nested attributes, you need to declare an attribute helper to work with - in this case :host_scans_attributes (or :hostScans_attributes if declared as camelcase) like this:

class Scan < ActiveRecord::Base
  attr_accessible :description, :end_time, :start_time, :raw, :host_scans_attributes
  has_many :host_scans, dependent: :destroy
  accepts_nested_attributes_for :host_scans, :allow_destroy => true
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I'm sure I'm closer, but it's still not working. Same basic problem: undefined method `host_scans_attributes' for #<Scan:0x23b97b8>
And you added :host_scans_attributes to the attr_accessible list, right?
yes. attr_accessible :description, :endTime, :startTime, :raw, :host_scans_attributes
Is it possible this isn't supposed to be in the model?
This certainly belongs in the model, can you repost the revised code? Something must be missing somewhere...
|
0

You're using attr_accessible in your model, which is basically a whitelist of all the attributes which can be mass assigend. So you need to add the attributes there as well...

1 Comment

That would be attr_accessible :description, :endTime, :startTime, :raw, :host_scans_attributes, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.