gpt4 book ai didi

ruby-on-rails - ActionController::ParameterMissing(参数缺失或值为空:名称):

转载 作者:行者123 更新时间:2023-11-29 13:44:37 26 4
gpt4 key购买 nike

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

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

我不断收到“ActionController::ParameterMissing(缺少参数或值为空:name):”,即使它显示输入的是 name。 '"users"=>{"name"=>"asdf", "password"=>"Qq!1asdfasdf", "password_confirmation"=>"Qq!1asdfasdf"}, "commit"=>"提交"}(0.1 毫秒)

output from the console when attempting to sign in

用户 Controller

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

我已尝试故障排除,所有类似的问题都没有得到答案或修复。

编辑:问题更清晰

最佳答案

您需要使用 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

关于ruby-on-rails - ActionController::ParameterMissing(参数缺失或值为空:名称):,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50433038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com