ActiveModel::ForbiddenAttributesError when creating new user

后端 未结 7 1753
礼貌的吻别
礼貌的吻别 2020-11-22 15:59

I have this model in Ruby but it throws a ActiveModel::ForbiddenAttributesError

class User < ActiveRecord::Base
  attr_accessor :password
  v         


        
7条回答
  •  遇见更好的自我
    2020-11-22 16:40

    I guess you are using Rails 4. If so, the needed parameters must be marked as required.

    You might want to do it like this:

    class UsersController < ApplicationController
    
      def create
        @user = User.new(user_params)
        # ...
      end
    
      private
    
      def user_params
        params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password)
      end
    end
    

提交回复
热议问题