简体   繁体   中英

Stack level too Ruby on rails

am fairly new to ruby so am not too sure what going wrong. Am getting an error saying "stack level too deep" am trying to register a new user

here is my code:

  class UsersController < ApplicationController
  def new
  @user = User.new
  end

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

private

  def params
    params.require(:user).permit(:firstName, :secondName, :activated, :email, :password)
  end

end

Users.rb

class User < ActiveRecord::Base

  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_presence_of :activated
  validates_presence_of :firstName
  validates_presence_of :secondName
  validates_uniqueness_of :email

  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

Full Error is :

SystemStackError in UsersController#create stack level too deep

Your params method needs to be named something other than params , such as safe_params . Otherwise you're calling itself when you go params.require on the very next line.

The params.require is accessing a method called params that comes from within Rails. The params method you just def 'd is overriding the Rails params method.

Clarity edit: This is what it should look like:

def create
  @user = User.new(safe_params)
  ...
end

def safe_params
  params.require(:user).permit(:firstName, :secondName, :activated, :email, :password)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM