Devise error: Unpermitted parameters

泪湿孤枕 提交于 2021-01-28 14:07:47

问题


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

回答1:


You need to permit additional parameters via strong parameters. Since you have custom parameter username defined, you would have to permit the same:

before_action :configure_permitted_parameters, if: :devise_controller?

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



回答2:


You are trying to Register a user with the wrong 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. It should be directed to the 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



来源:https://stackoverflow.com/questions/45682243/devise-error-unpermitted-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!