2

I have created a Category class:

class Category < ActiveRecord::Base
    attr_accessible :name
    has_many :posts
end

When I created a new object:

category = Category.new(:name => "News")

I am getting this error:

`NoMethodError: undefined method 'attr_accessible' for Category(call 
'Category.connection' to establish a connection):Class  ...

How can I resolve this?

Undo
  • 25,519
  • 37
  • 106
  • 129
  • What rails version are you using? This method was removed from ActiveRecord, and replaced with strong params – Avdept Apr 08 '15 at 19:27

4 Answers4

1

This will fix your issue.

class Category < ActiveRecord::Base

    def user_params
      params.require(:name)
    end
    has_many :posts
end
Cleb
  • 25,102
  • 20
  • 116
  • 151
Ali Raza
  • 794
  • 7
  • 12
0

You want to use attr_accessor, not attr_accessible.

attr_accessor is a Ruby method that defines setter and getter methods, while attr_accessible lets you whitelist ActiveRecord attributes for mass assignment.

Community
  • 1
  • 1
infused
  • 24,000
  • 13
  • 68
  • 78
  • i'm learnning tutorial [learn ruby from scratch ](https://www.youtube.com/watch?v=gSf86udAntA&index=21&list=PLDmvslp_VR0xlwr5lAx2PDsZLu7oIOhpX) in Video , use attr_accessible – nắng huế yêu gái huế Apr 03 '15 at 03:46
  • Hmm, perhaps you are supposed to use attr_accessible. You may just need to install the https://github.com/rails/protected_attributes gem. – infused Apr 03 '15 at 03:51
  • @nắnghuếyêugáihuế, `attr_accessible` is no longer valid for rails 4, instead there's `strong_parameters` now, this video is probably using a rails 3 version, and you're probably using a rails 4+ version – Mohammad AbuShady Apr 03 '15 at 09:49
0

Use attr_accessor there.. it will help you to solve the issue

Samsad CV
  • 126
  • 10
0

Below should fix also. No need to put ::Base -- you can remove it.

class Category < ActiveRecord
  attr_accessor :name
  has_many :posts
end
Aziz Zoaib
  • 661
  • 8
  • 21