mgh
mgh

Reputation: 1021

ActionController::RoutingError (undefined method `attr_accessible')

In a rails project, I use inherited_resources 1.5.0 gem. I have below model:

forums_controller.rb:

class ForumsController < InheritedResources::Base
  respond_to :json
  skip_before_filter  :verify_authenticity_token
  def permitted_params
    params.permit(vocabulary: [:name])
  end
end

forum.rb:

class Forum < ActiveRecord::Base
  attr_accessible :name
end

Now when I want create new forum, I post params to rails project, In this state, I have a problem:

when I use attr_accessible :name in my forum model(forum.rb), I get below error in server log and forum don't save to database:

Started GET "/forums" for 127.0.0.1 at 2014-08-09 11:51:42 +0430

ActionController::RoutingError (undefined method 'attr_accessible' for #<Class:0x000000064c0468>):
  app/models/forum.rb:2:in '<class:Forum>'
  app/models/forum.rb:1:in '<top (required)>'
  app/controllers/forums_controller.rb:3:in '<top (required)>'

and when I remove attr_accessible :name in my forum model, new forum save to database with empty name. How can I fix this problem?

Upvotes: 1

Views: 800

Answers (1)

mgh
mgh

Reputation: 1021

I removed attr_accessible from forum.rb and change forums_controller.rb:

forums_controller.rb:

class ForumsController < InheritedResources::Base
  respond_to :json

  #replace permitted_params with forum_params
  #the name have to similar to controller name
  def forum_params
    params.require(:forum).permit(:name)
  end
end

forum.rb:

class Forum < ActiveRecord::Base
  #some validation
end

Upvotes: 0

Related Questions