【问题标题】:ActionController::ParameterMissing (param is missing or the value is empty: name):ActionController::ParameterMissing(参数丢失或值为空:名称):
【发布时间】:2018-05-20 08:34:56
【问题描述】:

在为 Ruby on Rails 应用程序实现我认为是简单的signup/login 系统时,结果与教程显示的不符。

我正在尝试使用 bcrypt 进行身份验证,使用 PostgreSQL 进行数据库。

我不断收到'ActionController::ParameterMissing (param is missing or the value is empty: name):',即使它会显示名称作为输入。 '"users"=>{"name"=>"asdf", "password"=>"Qq!1asdfasdf", "password_confirmation"=>"Qq!1asdfasdf"}, "commit"=>"Submit"} (0.1ms)

output from the console when attempting to sign in

用户控制器

class UsersController < ApplicationController


def new
end

def create
  user = User.new(
    name: params[:name],
    password: params[:password],
    password_confirmation: params[:password_confirmation])
  if user.save
    session[:user_id] = user.id
    redirect_to '/'
  else
    redirect_to '/signup'
  end
end

private

end

桌子

class UsersController < ApplicationController


def new
end

def create
  user = User.new(
    name: params[:name],
    password: params[:password],
    password_confirmation: params[:password_confirmation])
  if user.save
    session[:user_id] = user.id
    redirect_to '/'
  else
    redirect_to '/signup'
  end
end

private

end

和注册表单

class UsersController < ApplicationController


def new
end

def create
  user = User.new(
    name: params[:name],
    password: params[:password],
    password_confirmation: params[:password_confirmation])
  if user.save
    session[:user_id] = user.id
    redirect_to '/'
  else
    redirect_to '/signup'
  end
end

private

end

用户模型

class User < ActiveRecord::Base


    PASSWORD_FORMAT = /\A
    (?=.{10,})          # Must contain 10 or more characters
    (?=.*\d)           # Must contain a digit
    (?=.*[a-z])        # Must contain a lower case character
    (?=.*[A-Z])        # Must contain an upper case character
    (?=.*[[:^alnum:]]) # Must contain a symbol
    /x

    #formatting for password

    USERNAME_FORMAT = /\A[a-z0-9A-Z\-_]{2,15}\z/ #Can contain lowercase and upercase letters, numbers, - and _, must be between 2 and 15 length

    #username formatting

    validates :name,
    :presence => true,
    :uniqueness => true,
    :format => USERNAME_FORMAT

    validates :password, 
    :presence => true, 
    :format => PASSWORD_FORMAT,
    :confirmation => true, 
    :on  => create 

  has_secure_password
end

我试过troubleshooting,所有类似的问题都没有得到答案或修复。

编辑:更明确的问题

【问题讨论】:

  • create 中的redirect_to '/signup' 之前添加puts user.errors.full_messages。查看它在终端中的打印内容。它会告诉你为什么记录没有持久化。
  • ActionController::ParameterMissing(参数缺失或值为空:名称):app/controllers/users_controller.rb:20:in user_params' app/controllers/users_controller.rb:7:in create'
  • 因此,这意味着您为 name 传递的值根据验证为空/无效。
  • 有建议的修复方法吗?输入不是无效的,我不知道该怎么做。
  • 根据您的日志,您将此名称发送给控制器:"name"=&gt;"Qq!1asdfasdf"。但是您对名称进行了正则表达式验证,并且“Qq!1asdfasdf”没有通过USERNAME_FORMAT。尝试使用其他名称,我认为它应该可以工作。

标签: ruby-on-rails ruby postgresql bcrypt


【解决方案1】:

你需要像下面这样使用 rails Strong Parameter

class UsersController < ApplicationController

    def new
    end

    def create
      user = User.new(user_params)
      if user.save
        session[:user_id] = user.id
        redirect_to root_path
      else
        redirect_to new_user_path
      end
    end

    private

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

end

【讨论】:

  • 仍然没有到达那里,参数:{"utf8"=>"✓", "authenticity_token"=>"xl6lwi1CGGcuZIxjkvQsWqGrQwqtruLg9FheiC1UNZze5PEHAh9gXN4bZoGQ8gw4keBR/0QDRyAqx2b5hoIbBA==", "users"=> Qq!1asdfasdf", "password"=>"Qq!1asdfasdf", "password_confirmation"=>"Qq!1asdfasdf"}, "commit"=>"Submit"} (0.2ms) BEGIN 用户存在 (0.5ms) SELECT 1作为一个来自“用户”的地方,“用户”。“名称”是 NULL LIMIT 1 (0.2ms) ROLLBACK
  • name 输入字段没有传值,你可以在那里检查,
  • 我已经检查过,但我不知道解决方案是什么。
【解决方案2】:

我认为这是在表单和控制器之间传递数据的问题。
在您的日志中,您的用户参数如下所示:"users"=&gt;{"name"=&gt; ...} 但它应该是"user"

要在控制器和视图之间传递数据,您需要使用 @user 等实例变量来使 User 的新实例在视图中可用。 (source)

这样你的控制器应该是:

def new
  @user = User.new
end

def create
  @user = User.new(user_params)
  if @user.save
    session[:user_id] = @user.id
    redirect_to '/'
  else
    redirect_to '/signup'
  end
end

private

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

(使用 @fool-dev 等强参数解释)

那么在你看来,使用这个@user将参数传递给控制器​​:

<%= form_for @user do |f| %>
  //...the form
<% end %>

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2021-11-20
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多