S
S
shmigawhite2014-02-19 17:43:30
Ruby on Rails
shmigawhite, 2014-02-19 17:43:30

Why does the error "Can't mass-assign protected attributes for" appear on save after migrating to Rails 4 in Rails Admin?

The problem is that earlier I wrote something like this in models:
attr_accessible :name, :value, :var, as: :admin
and I wrote in rails_admin.rb
config.attr_accessible_role { :admin }
But with the new version, the attr_accessible_role method was removed. I understand that RoR 4 uses strong parameters. But I didn't want to change a lot of code, so I just added the 'protected_attributes' gem. But there was a problem with the Rails admin when saving, it gives an error:
Can't mass-assign protected attributes for.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arkady Butermanov, 2014-02-19
@Arkadey

The bottom line is that the attributes that can be edited in batches must be defined in the controller, not in the model.
To get rid of your error, call the permit method on the params hash in the controller and pass it a list of editable attributes. More or less like this:

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  private

  def person_params
    params.require(:person).permit(:name, :age)
  end
end

G
Georgy Khromchenko, 2014-04-15
@Mox

There was another gem for quick migration of old applications.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question