Ruby-on-rails 参数缺失或值为空:用户 rails 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24243253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:43:13  来源:igfitidea点击:

param is missing or the value is empty: user rails 4

ruby-on-railsrubyruby-on-rails-4

提问by theJava

I am getting the following error, when i try to submit my registration form. param is missing or the value is empty: user

当我尝试提交注册表时,出现以下错误。参数缺失或值为空:用户

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

Below is the request:

下面是请求:

{"utf8"=>"?",
 "authenticity_token"=>"YX0+4AeWlGWJiQZgbhV9cxi6TUCoibZfwh95BrK9iCQ=",
 "name"=>"asasa",
 "email"=>"[email protected]",
 "password"=>"[FILTERED]",
 "confirm_password"=>"[FILTERED]",
 "commit"=>"Register"}

Below is my view

下面是我的看法

<%= form_tag users_path, class: "form-horizontal", id: "signupform" do %>

<%= end %>

回答by Max Williams

there is no params[:user]there, that's what the error is telling you. Are you perhaps expecting your params to look like this?

没有params[:user],这就是错误告诉你的。您是否希望您的参数看起来像这样?

{"utf8"=>"?",
 "authenticity_token"=>"YX0+4AeWlGWJiQZgbhV9cxi6TUCoibZfwh95BrK9iCQ=",
 "user" => {"name"=>"asasa",
            "email"=>"[email protected]",
            "password"=>"[FILTERED]",
            "confirm_password"=>"[FILTERED]"
 },"commit"=>"Register"}

(i've added some indentation to highlight the structure).

(我添加了一些缩进来突出结构)。

If so, then you need to amend the form that submitted these params. Either use form_for @user, which will automatically set the input tags' nameattributes to user[name], user[email]etc, or manually set the field's names yourself, to user[name]etc.

如果是这样,那么您需要修改提交这些参数的表单。无论是使用 form_for @user,它会自动设置输入标签的name属性user[name]user[email]等等,或者手动设置字段的名称自己,user[name]等等。