1

I'd like to initiate a new record with some default values.

@test = "#{@fb_page['id']}, slug: #{slug}, title: #{@fb_page['name']}"
@frame = Frame.new(:fb_page_id => @fb_page['id'].to_i, :slug => slug, :title => @fb_page['name'], :theme => 'default')

@test shows all I want:

<the-page-id>, slug: mrs-test, title: Mrs. Test 

But a few of the default values for the @frame are nil!

# @frame.inspect: 
#<Frame id: nil, slug: nil, title: "Mrs. Test", theme: "default", fb_page_id: nil, created_at: nil, updated_at: nil>  

Could anyone please explain me why it doesn't take some of the variables? The string "default" gets through as well as@fb_page['name'].

Thanks in advance!

0

1 Answer 1

5

This typically happens when you don't list those attributes under the attr_accessible section of the model.

attr_accessible :fb_page_id, :slug, :title, :theme

Rails has mass-assignment protection to prevent malicious users from mass-assigning certain model attributes. So when you pass in attributes to a Model.new() method, the only attributes that will persist will be ones that are listed in attr_accessible (unless you haven't protected your models yet).

If you want to leave those attributes protected (as they currently are), you need to set those attributes using dot notation:

@frame = Frame.new
@frame.fb_page_id = @fb_page['id'].to_i
@frame.slug = slug
@frame.title = @fb_page['name']
@frame.theme = 'default'
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Your are too good! That was exactly the point! My slug was originaly named 'unique_name' and that was in the list of attr_accessible. Thanks a lot! Also good to know that I would have made it with dot notation. Because I thougth I can assigne all attributes but not from 'outside'. Thanks!!!

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.