Undefined Method attr_accessible

后端 未结 3 1779
挽巷
挽巷 2020-12-01 04:17

I am somewhat new to rails and I am trying to create a User login. I went through the tutorial found here. At the end it had me add \"attr_accessible\" for mass assignment

相关标签:
3条回答
  • 2020-12-01 04:46

    Make sure you are installed gem 'protected_attributes', that this gem is present in your gemfile and run bundle install terminal. Then restart your server.

    0 讨论(0)
  • 2020-12-01 05:04

    No mass assignment allowed for Rails 4.1

    You will have to try something like this.

    class Person
      has_many :pets
      accepts_nested_attributes_for :pets
    end
    
    class PeopleController < ActionController::Base
      def create
        Person.create(person_params)
      end
    
      ...
    
      private
    
        def person_params
          # It's mandatory to specify the nested attributes that should be whitelisted.
          # If you use `permit` with just the key that points to the nested attributes hash,
          # it will return an empty hash.
          params.require(:person).permit(:name, :age, pets_attributes: [ :name, :category ])
        end
    end
    

    Refer

    https://github.com/rails/strong_parameters

    0 讨论(0)
  • 2020-12-01 05:09

    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.

    0 讨论(0)
提交回复
热议问题