1

I am new on rails. Below is what i am trying

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

but i am not sure how to use attr_accessible in rails 4+ in my above scnerio . I was doing as per old .Please suggest

Torakami
  • 177
  • 2
  • 12

3 Answers3

2

In Rails 4 + , there is no mass assignment support.

You have to pass in controller and use that like,

def create
  Category.create(category_params)
end

private

def category_params
  params.require(:category).permit(:name)
end

then call the category_params method in your controller actions.

rick
  • 1,675
  • 3
  • 15
  • 28
1

For rails 4 and above you do not need to use this just permit your attributes in controller

 def category_params
   params.require(:category).permit(:name)
 end
Gaurav Gupta
  • 1,181
  • 10
  • 15
1

In rails 4 attr_accessible is deprecated you should use strong params

Rails 4, Strong Parameters, and Deprecation of the attr_accessible Macro

rick
  • 1,675
  • 3
  • 15
  • 28
Thorin
  • 2,004
  • 1
  • 19
  • 30