GithubHelp home page GithubHelp logo

Comments (10)

chr4 avatar chr4 commented on August 21, 2024

Note: I'm using the proteced_attributed gem.

from the_role.

the-teacher avatar the-teacher commented on August 21, 2024

https://github.com/the-teacher/the_role/blob/master/lib/the_role.rb#L11

TheRole.create_admin_role!

from the_role.

chr4 avatar chr4 commented on August 21, 2024

Hi, thanks for the response.
Yeah, I tried running that manually via rails console, but it threw the same error:

2.1.0p0 :011 > Role.create(name: :admin, title: 'Role for admin', description: 'This user can do anything')
WARNING: Can't mass-assign protected attributes for Role: name, title, description

W, [2014-01-16T19:48:35.259806 #11147]  WARN -- : WARNING: Can't mass-assign protected attributes for Role: name, title, description

   (0.1ms)  begin transaction
D, [2014-01-16T19:48:35.260084 #11147] DEBUG -- :    (0.1ms)  begin transaction
  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."name" IS NULL LIMIT 1
D, [2014-01-16T19:48:35.261392 #11147] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."name" IS NULL LIMIT 1
  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."title" IS NULL LIMIT 1
D, [2014-01-16T19:48:35.262400 #11147] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."title" IS NULL LIMIT 1
   (0.1ms)  rollback transaction
D, [2014-01-16T19:48:35.263111 #11147] DEBUG -- :    (0.1ms)  rollback transaction
 => #<Role id: nil, name: nil, title: nil, description: nil, the_role: nil, created_at: nil, updated_at: nil>

TheRole.create_admin_role! gives a similar error:

2.1.0p0 :014 >   TheRole.create_admin_role!
  Role Load (0.2ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' ORDER BY "roles"."id" ASC LIMIT 1
D, [2014-01-16T19:49:54.968292 #11147] DEBUG -- :   Role Load (0.2ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' ORDER BY "roles"."id" ASC LIMIT 1
WARNING: Can't mass-assign protected attributes for Role: name, title, description

W, [2014-01-16T19:49:54.970585 #11147]  WARN -- : WARNING: Can't mass-assign protected attributes for Role: name, title, description

   (0.1ms)  begin transaction
D, [2014-01-16T19:49:54.970782 #11147] DEBUG -- :    (0.1ms)  begin transaction
  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."name" = 'admin' LIMIT 1
D, [2014-01-16T19:49:54.971528 #11147] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."name" = 'admin' LIMIT 1
  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."title" IS NULL LIMIT 1
D, [2014-01-16T19:49:54.972522 #11147] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."title" IS NULL LIMIT 1
   (0.1ms)  rollback transaction
D, [2014-01-16T19:49:54.973794 #11147] DEBUG -- :    (0.1ms)  rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Title can't be blank, Description can't be blank
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/validations.rb:57:in `save!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:275:in `block in save!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:275:in `save!'
  from .rvm/gems/ruby-2.1.0/gems/protected_attributes-1.0.5/lib/active_record/mass_assignment_security/validations.rb:17:in `create!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation.rb:129:in `block in create!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation.rb:270:in `scoping'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation.rb:129:in `create!'
  from .rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation.rb:137:in `first_or_create!'
  from .rvm/gems/ruby-2.1.0/gems/the_role-2.3/lib/the_role.rb:12:in `create_admin_role!'
  from (irb):14
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

Note the WHERE "roles"."name" IS NULL and the mass-asignment warning WARNING: Can't mass-assign protected attributes for Role: name, title, description. Any further ideas?

from the_role.

the-teacher avatar the-teacher commented on August 21, 2024

hm!

try to add

attr_accessible :name, :title, :description

in Role model in your application

from the_role.

chr4 avatar chr4 commented on August 21, 2024

After using this app/model/role.rb

class Role < ActiveRecord::Base
  attr_accessible :name, :title, :description

  acts_as_role
end

rails generate the_role admin fails with another error now:

D, [2014-01-16T20:23:51.099788 #11700] DEBUG -- :   Role Load (0.1ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' ORDER BY "roles"."id" ASC LIMIT 1
D, [2014-01-16T20:23:51.104608 #11700] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-01-16T20:23:51.113567 #11700] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."name" = 'admin' LIMIT 1
D, [2014-01-16T20:23:51.114107 #11700] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE "roles"."title" = 'role for admin' LIMIT 1
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
D, [2014-01-16T20:23:51.201496 #11700] DEBUG -- :   SQL (3.7ms)  INSERT INTO "roles" ("created_at", "description", "name", "the_role", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Thu, 16 Jan 2014 19:23:51 UTC +00:00], ["description", "this user can do anything"], ["name", "admin"], ["the_role", "{}"], ["title", "role for admin"], ["updated_at", Thu, 16 Jan 2014 19:23:51 UTC +00:00]]
D, [2014-01-16T20:23:51.203069 #11700] DEBUG -- :    (1.1ms)  commit transaction
D, [2014-01-16T20:23:51.204063 #11700] DEBUG -- :    (0.0ms)  begin transaction
W, [2014-01-16T20:23:51.206280 #11700]  WARN -- : WARNING: Can't mass-assign protected attributes for Role: the_role

D, [2014-01-16T20:23:51.207400 #11700] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."name" = 'admin' AND "roles"."id" != 1) LIMIT 1
D, [2014-01-16T20:23:51.207869 #11700] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."title" = 'role for admin' AND "roles"."id" != 1) LIMIT 1
D, [2014-01-16T20:23:51.208450 #11700] DEBUG -- :    (0.0ms)  commit transaction
.rvm/gems/ruby-2.1.0/gems/the_role-2.3/app/models/concerns/role.rb:57:in `create_rule': undefined method `[]' for nil:NilClass (NoMethodError)
  from .rvm/gems/ruby-2.1.0/gems/the_role-2.3/lib/generators/the_role/the_role_generator.rb:51:in `create_admin_role'
  from .rvm/gems/ruby-2.1.0/gems/the_role-2.3/lib/generators/the_role/the_role_generator.rb:14:in `generate_controllers'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `block in invoke_all'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `each'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `map'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/invocation.rb:127:in `invoke_all'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/group.rb:233:in `dispatch'
  from .rvm/gems/ruby-2.1.0/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/generators.rb:156:in `invoke'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/generate.rb:11:in `<top (required)>'
  from .rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
  from .rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
  from .rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
  from .rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:48:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

As well as TheRole.create_admin_role!

  Role Load (0.1ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' ORDER BY "roles"."id" ASC LIMIT 1
D, [2014-01-16T20:24:04.495972 #11743] DEBUG -- :   Role Load (0.1ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' ORDER BY "roles"."id" ASC LIMIT 1
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
   (0.1ms)  begin transaction
D, [2014-01-16T20:24:04.581051 #11743] DEBUG -- :    (0.1ms)  begin transaction
WARNING: Can't mass-assign protected attributes for Role: the_role

W, [2014-01-16T20:24:04.584148 #11743]  WARN -- : WARNING: Can't mass-assign protected attributes for Role: the_role

  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."name" = 'admin' AND "roles"."id" != 1) LIMIT 1
D, [2014-01-16T20:24:04.591668 #11743] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."name" = 'admin' AND "roles"."id" != 1) LIMIT 1
  Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."title" = 'role for admin' AND "roles"."id" != 1) LIMIT 1
D, [2014-01-16T20:24:04.592253 #11743] DEBUG -- :   Role Exists (0.1ms)  SELECT 1 AS one FROM "roles" WHERE ("roles"."title" = 'role for admin' AND "roles"."id" != 1) LIMIT 1
   (0.2ms)  commit transaction
D, [2014-01-16T20:24:04.593687 #11743] DEBUG -- :    (0.2ms)  commit transaction
NoMethodError: undefined method `[]' for nil:NilClass
  from .rvm/gems/ruby-2.1.0/gems/the_role-2.3/app/models/concerns/role.rb:57:in `create_rule'
  from .rvm/gems/ruby-2.1.0/gems/the_role-2.3/lib/the_role.rb:17:in `create_admin_role!'
  from (irb):1
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
  from .rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

But creating it manually works:

2.1.0p0 :002 > Role.first_or_create!(name: :admin, title: 'Role for admin', description: 'This user can do anything')
  Role Load (0.3ms)  SELECT "roles".* FROM "roles" ORDER BY "roles"."id" ASC LIMIT 1
D, [2014-01-16T20:24:15.132987 #11743] DEBUG -- :   Role Load (0.3ms)  SELECT "roles".* FROM "roles" ORDER BY "roles"."id" ASC LIMIT 1
 => #<Role id: 1, name: "admin", title: "role for admin", description: "this user can do anything", the_role: "{}", created_at: "2014-01-16 19:23:51", updated_at: "2014-01-16 19:23:51">
2.1.0p0 :004 > Role.all
  Role Load (0.2ms)  SELECT "roles".* FROM "roles"
D, [2014-01-16T20:24:29.713838 #11743] DEBUG -- :   Role Load (0.2ms)  SELECT "roles".* FROM "roles"
 => #<ActiveRecord::Relation [#<Role id: 1, name: "admin", title: "role for admin", description: "this user can do anything", the_role: "{}", created_at: "2014-01-16 19:23:51", updated_at: "2014-01-16 19:23:51">]>

from the_role.

the-teacher avatar the-teacher commented on August 21, 2024

attr_accessible :name, :title, :description, :the_role

from the_role.

the-teacher avatar the-teacher commented on August 21, 2024

WARNING: Can't mass-assign protected attributes for Role: the_role

from the_role.

chr4 avatar chr4 commented on August 21, 2024

Ah, that helped. After making sure that I have the following in my user model, it seems to work

class User < ActiveRecord::Base
  has_role
  attr_accessible :role
end

Maybe it's a good idea to create a comment in https://github.com/the-teacher/the_role/blob/master/spec/dummy_app/app/models/user.rb that the attr_accessible is necessaty when using protected_attributes with rails 4?

Thanks!

from the_role.

the-teacher avatar the-teacher commented on August 21, 2024

thanks! I'll do that
nave a nice day!

from the_role.

seuros avatar seuros commented on August 21, 2024

Rails 4 deprecated protected_attributes and use strong_parametres. protected_attributes is insecure and should be migrated. I don't think we should support it in the future versions of gem

from the_role.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.