简体   繁体   English

UsersController#中的ArgumentError创建错误的参数数量(给定0,应为1)

[英]ArgumentError in UsersController#create wrong number of arguments (given 0, expected 1)

First up, I'm using rails 5.1.2. 首先,我使用rails 5.1.2。 I've used Michael Hartl's tutorial as a starting point for a web app and I've run into a snag. 我已经将Michael Hartl的教程用作Web应用程序的起点,但遇到了麻烦。

When I try to sign up to the website while running a dev server on localhost, I get an error after I submit the user details. 当我在localhost上运行开发服务器时尝试注册网站时,提交用户详细信息后出现错误。

 ArgumentError in UsersController#create wrong number of arguments (given 0, expected 1)
 Extracted source (around line #21):

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url

 Rails.root: /home/krefey/workspace/gaming-sonar
 Application Trace | Framework Trace | Full Trace

 app/controllers/users_controller.rb:21:in `create'

I think it is because I have installed ActiveAdmin and Devise and that is causing issues with the user model that has been setup. 认为这是因为我已经安装了ActiveAdmin和Devise,并导致已设置的用户模型出现问题。

user.rb user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save :downcase_email
  before_create :create_activation_digest
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 10 }, allow_nil: true

  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Activates an account.
  def activate
    update_columns(activated: true, activated_at: Time.zone.now.to_datetime)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
      update_columns(reset_digest:  User.digest(reset_token), reset_sent_at: Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

  # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end


  private

    # Converts email to all lower-case.
    def downcase_email
      email.downcase!
    end

    # Creates and assigns the activation token and digest.
    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end
end

user_controller user_controller

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    @users = User.where(activated: true).paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    redirect_to root_url and return unless @user.activated?
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url



  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private

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

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

    # Confirms an admin user.
    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
end

_form.html.erb _form.html.erb

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', object: @user %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>

  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

new.html.erb new.html.erb

<% provide(:title, 'Sign up') %>
<% provide(:button_text, 'Create my account') %>
<h1>Sign up</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= render 'form' %>
  </div>
</div>

Now, from what I can tell, it feels like that when the create function is being called, it is supposed to be assigning the parameters (user_params) to the @user variable. 现在,据我所知,感觉就像在调用create函数时,它应该将参数(user_params)分配给@user变量。

I strongly suspect that the devise line at the start of the user.rb file that I added is the culprit, but I'm not sure why. 我强烈怀疑在添加的user.rb文件开头的设计行是罪魁祸首,但我不确定为什么。

You passed incorrect parameter in User.digest function, activation_token is in self.activation_token . 您在User.digest函数中传递了不正确的参数, activation_tokenself.activation_token

try to change below line in user.rb model: 尝试更改user.rb模型中的以下行:

self.activation_digest = User.digest(activation_token)

with

self.activation_digest = User.digest(self.activation_token)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UsersController#create中的ArgumentError; 参数数量错误(2代表0) - ArgumentError in UsersController#create; Wrong number of arguments (2 for 0) Rails - UsersController中的ArgumentError #create - 参数太少 - Rails - ArgumentError in UsersController#create - too few arguments FactoryGirl ArgumentError:参数数量错误(给定1,预期为0) - FactoryGirl ArgumentError: wrong number of arguments (given 1, expected 0) ArgumentError:参数数量错误(给定0,应为1..2) - ArgumentError: wrong number of arguments (given 0, expected 1..2) ArgumentError(参数数量错误(给定 1,预期为 2)) - ArgumentError (wrong number of arguments (given 1, expected 2)) Rails 中的 ArgumentError(arguments 的编号错误(给定 5,预期为 1)) - ArgumentError (wrong number of arguments (given 5, expected 1)) in Rails Rails-ArgumentError(参数数量错误(给定1,预期为0)): - Rails - ArgumentError (wrong number of arguments (given 1, expected 0)): Rails-ArgumentError给定的参数个数错误3,预期为2 - Rails - ArgumentError wrong number of arguments given 3, expected 2 ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) ArgumentError错误的参数数量(给定0,应为1)-Scss文件 - ArgumentError wrong number of arguments (given 0, expected 1) - Scss files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM