簡體   English   中英

ActiveModel :: ForbiddenAttributesError Ruby on Rails

[英]ActiveModel::ForbiddenAttributesError Ruby On Rails

我在Ruby On rails中創建了一個項目,並且有這樣的控制器用戶:

class UsersController < ApplicationController
    before_action :set_user, only: [:edit, :update]
    def new
        @user = User.new
    end

    def create
        @user = User.new(params[:user])
        if @user.save
            redirect_to root_url, :notice => "Signed up!"
        else
        render "new"
        end
    end

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

和模型用戶是這樣的:

class User < ActiveRecord::Base
  #attr_accessible :email, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

當我嘗試創建新用戶時,出現錯誤:ActiveModel :: ForbiddenAttributesError

參數:

{"utf8"=>"✓",
 "authenticity_token"=>"74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=",
 "user"=>{"email"=>"henio180@interia.pl",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "button"=>""}

我正在使用Ruby on Rails 4.0.3。

在您的create方法更改中:

@user = User.new(params[:user])

@user = User.new(user_params)

盡管您正確地創建了用於設置強參數的方法,但實際上並沒有使用它。 在Rails指南中有關於使用強參數的詳細信息: http : //guides.rubyonrails.org/action_controller_overview.html#strong-parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM