Undefined Method Attr_Accessible

Undefined Method attr_accessible

No mass assignment allowed for Rails 4.1

instead of having attr_accessible :username, :email, :password, :password_confirmation in your model, use strong parameters.
You'll do this in your UsersController:

    def user_params
params.require(:user).permit(:username, :email, :password, :password_confirmation)
end

then call the user_params method in your controller actions.

Undefined method attr_accessible error for User

attr_accessible is not available for Rails version 4+. You would have to go with strong parameters.

With Strong Parameters, attribute whitelisting has been moved to controller level. Remove the attr_accessible call from your model.

Here is an example in Rails Guide of how to use Strong Parameters

In your case you can do something like this:

class UsersController < ApplicationController
## ...
def create
@user = User.new(user_params) ## Invoke user_params method
if @user.save
redirect_to @user, notice: 'User was successfully created.'
else
render action: 'new'
end
end
## ...

private
## Strong Parameters
def user_params
params.require(:user).permit(:name, :password_digest, :password, :password_confirmation)
end
end

You can take a note of @Frederick comment below my answer,

you can still use attr_accessible but it has been extracted into the
protected_attributes gem (although clearly strong parameters is the
way forwards)

rails undefined method `attr_accessible'

From the Rails Guides:

Rails 4.0 has removed attr_accessible and attr_protected feature in
favor of Strong Parameters. You can use the Protected Attributes gem
for a smooth upgrade path.

Source: http://guides.rubyonrails.org/upgrading_ruby_on_rails.html

You will probably want to keep the attr_accessible method or you will run into errors when creating users with a sign up form. You can bundle the protected_attributes gem to give you access to those methods in your models.

Protected Attributes: https://github.com/rails/protected_attributes

Cannot instance object with attr_accessible in Rails

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.

Ruby on Rails undefined method `attr_accessible' and 'before_save'?

Is your User class inheriting from ActiveRecord::Base?

It should look like this:

class User < ActiveRecord::Base

ActionController::RoutingError (undefined method `attr_accessible')

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

How is attr_accessible used in Rails 4?

Rails 4 now uses strong parameters.

Protecting attributes is now done in the controller. This is an example:

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

private

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

No need to set attr_accessible in the model anymore.

Dealing with accepts_nested_attributes_for

In order to use accepts_nested_attribute_for with strong parameters, you will need to specify which nested attributes should be whitelisted.

class Person
has_many :pets
accepts_nested_attributes_for :pets
end

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

# ...

private

def person_params
params.require(:person).permit(:name, :age, pets_attributes: [:name, :category])
end
end

Keywords are self-explanatory, but just in case, you can find more information about strong parameters in the Rails Action Controller guide.

Note: If you still want to use attr_accessible, you need to add protected_attributes to your Gemfile. Otherwise, you will be faced with a RuntimeError.



Related Topics



Leave a reply



Submit