简体   繁体   English

设计错误:不允许的参数

[英]Devise error: Unpermitted parameters

How to register a user with username devise?如何使用用户名设计注册用户? What I have so far throws me the following error:到目前为止,我遇到了以下错误:

Error: 
Processing by Devise::SessionsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oa1sf2dgKU/VpgOAI5ocnM8e5gNkuswxX9KKpTLFUZmUwtO40CFoseZiYQ3eQZ71bTw9R3aFOhpbD5EZcAVcGA==", "user"=>{"username"=>"MCarpintini", "email"=>"carpintinimatias@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Register"}
Unpermitted parameters: :username, :password_confirmation

Registration form登记表

.ui.middle.aligned.center.aligned.grid
  .column
    =form_for(resource, html: {class:"ui large form"}, as: resource_name,url: session_path(resource_name)) do |f|
      .ui.stacked.segment
        .field
          .ui.left.icon.input
            %i{class:"user icon"}
            =f.text_field :username, placeholder: "Username"
        .field
          .ui.left.icon.input
            %i{class:"mail icon"}
            =f.email_field :email, placeholder:"Email"
        .field
          .ui.left.icon.input
            %i{class:"lock icon"}
            =f.password_field :password, placeholder:"Password"

        .field
          .ui.left.icon.input
            %i{class:"repeat icon"}
            =f.password_field :password_confirmation, placeholder:"Password Confirmation"

        =f.submit "Register", class:"ui fluid large teal submit button"
      .ui.error.message
    .ui.message
      Do you have account?
      =link_to "Login", new_user_session_path

User model用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

end

User table structure.用户表结构。

create_table "users", force: :cascade do |t|
    t.string "username", default: "", null: false
    t.string "avatar_file_name", default: "", null: false
    t.string "avatar_content_type", default: "", null: false
    t.integer "avatar_file_size", null: false
    t.datetime "avatar_updated_at", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

You need to permit additional parameters via strong parameters .您需要通过strong parameters允许其他参数 Since you have custom parameter username defined, you would have to permit the same:由于您定义了自定义参数username ,因此您必须允许相同的:

before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :password_confirmation])
end

You are trying to Register a user with the wrong url .您正在尝试使用错误的url注册用户。 There's a problem with this line:这一行有一个问题:

=form_for(resource, html: {class:"ui large form"}, as: resource_name,url: session_path(resource_name)) do |f|

url: session_path(resource_name) sends the data to SessionsController. url: session_path(resource_name)将数据发送到 SessionsController。 It should be directed to the RegistrationsController.它应该被定向到 RegistrationsController。

Try replacing the form submission url to registration_path or new_registration_path or new_user_registration_path , whichever is defined in your _links.html.erb file尝试将表单提交url替换为registration_pathnew_registration_pathnew_user_registration_path ,以您的_links.html.erb文件中定义的为准

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

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