Search code examples
ruby-on-railsmass-assignment

how to avoid Can't mass-assign protected attributes issue from controller


let's say, i've got 2 apps which use the same database(app), and i've got a field say: cool, so i want to use this field in first app but not in second. so how to avoid this error Can't mass-assign protected attributes without using attr_accessible in database(app) ?


Solution

  • I'm not sure if I understand, but this is what I ususally do:

    protected_attribute = params[:blog_post].delete(:protected_attribute)
    
    @blog_post = BlogPost.new(params[:blog_post])
    @blog_post.protected_attribute = protected_attribute
    if @blog_post.save
      # ...
    else
      # ...
    end
    

    It's ugly, but circumvents the mass-assign protection.

    Update: You also have to remove the protected attribute from the params.