Daniela
Daniela

Reputation: 366

rails 4 ActiveModel::ForbiddenAttributesError attributes as variable fail, attributes spelled out success

in model Artist:

Edited to reflect misunderstandings in comments.

Dog.poop is just my silly way of using colorize to get a more visible puts output. It only eats strings, that is why Dog.poop("#{}")

Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"

is just to show I checked if attributes is indeed == with the {"firstname etc Hash. is true (=== is true too)

because of what follows

Example 1: Trying to make a new Artist object, feeding in variable attributes from function argument. Fails.

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    Dog.poop("#{self.new(attributes)}")
    # --> ActiveModel::ForbiddenAttributesError
end

BUT:

Example 2: Trying to make a new Artist object, feeding in the {"firstname etc Hash. Succeeds.

This is weird, if attributes=={{"firstname etc Hash is true!

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    Dog.poop("#{self.new({"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"})}")
    # --> #<Artist:0x007fe39ee2cc08>
end

AND this works too:

Example 3: If I make the object by passing the attributes into a block and adding them one by one, then also: Success.

WHY? why Fail in ex 1, but success in ex2 and ex3?

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    self.new do |artist|
        attributes.each do |key, value|
            artist[key] = value
        end
        Dog.poop("#{artist}")
        # --> #<Artist:0x007fe39ee2cc08>
    end
end

WHY? What am I not getting here? Thanks!

Upvotes: 0

Views: 36

Answers (1)

Beartech
Beartech

Reputation: 6421

It looks like you are trying to use == for variable assignment then use variable interpolation to get those into the object.

Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}" 

You are also missing ) at the end of the above line and have an extra double quote at the very end.

I think what you really meant to do is:

    Dog.poop("#{attributes = {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}")

$> attributes
     => {"firstname"=>"Firstname",
     "lastname"=>"Lastname",
     "description"=>"test",
     "document_id"=>"",
     "email"=>"",
     "mobile"=>"",
     "url"=>"",
     "soloist"=>"1",
     "performance_order"=>"1"}

Still not sure why you are doing it that way, it doesn't make sense to me, but maybe you can explain. If attributes is being passed to the method then why would you then override it?

Or are you checking to see if the attributes being passed in matches what you were expecting, then saving? In that case I think it's just a case of the missing " at the beginning before #{attributes....

Dog.poop("#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}")

Is basically just:

Dog.poop(true)

Why are you doing that, it is not clear from your code.

Upvotes: 1

Related Questions