设计错误:未允许的参数

时间:2017-08-14 20:04:45

标签: ruby-on-rails devise

如何使用用户名设计注册用户? 到目前为止,我发现了以下错误:

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

注册表

.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

用户模型

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

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

end

用户表格结构。

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

2 个答案:

答案 0 :(得分:2)

您需要通过strong parameters允许其他参数。由于您已定义自定义参数username,因此您必须允许相同的内容:

before_action :configure_permitted_parameters, if: :devise_controller?

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

答案 1 :(得分:0)

您正在尝试使用错误的url注册用户。这条线有问题:

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

url: session_path(resource_name)将数据发送到SessionsController。应该将其定向到RegistrationsController。

尝试将表单提交url替换为registration_path文件中定义的new_registration_pathnew_user_registration_path_links.html.erb