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!
This typically happens when you don’t list those attributes under the
attr_accessiblesection of the model.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 inattr_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: