如何解决Rails 设计:configure_permitted_parameters 未按预期工作
我正在使用带有 API 版本控制的设计,当我尝试使用设计创建新用户时,它给了我(电子邮件不能为空,密码不能为空),我在网上搜索了很多解决方案,我找到了我需要为参数添加一个消毒剂,所以我将它添加到我的 application_controller.rb 但不幸的是它不起作用。
我会粘贴代码,请告诉我有什么问题?
提示:我需要原样的路线(
POST /api/v1/users(.:format) ---> devise/registrations#create {:format=>:json}
)
应用层次结构
- 应用
- 控制器
- api
- v1
- 担忧
- api_controller.rb
- v2
- v1
- application_controller.rb
- api
- 模型
- user.rb
- application_record.rb
- 控制器
- 配置
- 数据库
- ...
app/controller/application_controller.rb
class ApplicationController < ActionController::API
before_action :configure_permitted_parameters,if: :devise_controller?
respond_to :json
# Devise code
protected
# Devise methods
# Authentication key(:username) and password field will be added automatically by devise.
def configure_permitted_parameters
added_attrs = [:name,:email]
devise_parameter_sanitizer.permit(:sign_up,keys: added_attrs)
devise_parameter_sanitizer.permit(:account_update,keys: added_attrs)
end
end
config/routes.rb
require 'api_constraints.rb'
Rails.application.routes.draw do
# For details on the DSL available within this file,see https://guides.rubyonrails.org/routing.html
namespace :api,defaults: { format: :json } do
namespace :v1,constraints: ApiConstraints.new(version: 1,default: true) do
# for APIs version,we will use the default devise controllers
devise_for :users,controllers: {
:sessions => "devise/sessions",:passwords => "devise/passwords",:registrations => "devise/registrations",:confirmations => "devise/confirmations",:unlocks => "devise/unlocks"
}
end
end
end
config/initializers/devise.rb
Devise.setup do |config|
require 'devise/orm/active_record'
config.case_insensitive_keys = [:email]
config.strip_whitespace_keys = [:email]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 12
config.reconfirmable = true
config.expire_all_remember_me_on_sign_out = true
config.password_length = 6..128
config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
config.reset_password_within = 6.hours
config.sign_out_via = :delete
end
db/migrate/20210615223352_devise_create_users.rb
# frozen_string_literal: true
class DeviseCreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name,null: false,default: ""
t.datetime :date_of_birth
t.string :status
t.string :type
t.string :nickname
## Database authenticatable
t.string :email,default: ""
t.string :encrypted_password,default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
# Trackable
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
# Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
# Lockable
t.integer :failed_attempts,null: false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at
t.timestamps null: false
end
add_index :users,:email,unique: true
add_index :users,:reset_password_token,:confirmation_token,:unlock_token,unique: true
end
end
db/schema.rb
ActiveRecord::Schema.define(version: 2021_06_15_223352) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users",force: :cascade do |t|
t.string "name",default: "",null: false
t.datetime "date_of_birth"
t.string "status"
t.string "type"
t.string "nickname"
t.string "email",null: false
t.string "encrypted_password",null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count",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.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts",null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at",precision: 6,null: false
t.datetime "updated_at",null: false
t.index ["confirmation_token"],name: "index_users_on_confirmation_token",unique: true
t.index ["email"],name: "index_users_on_email",unique: true
t.index ["reset_password_token"],name: "index_users_on_reset_password_token",unique: true
t.index ["unlock_token"],name: "index_users_on_unlock_token",unique: true
end
end
lib/api_constraints.rb
class ApiConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
@default ||
(req.respond_to?('headers') &&
req.headers.key?('Accept') &&
req.headers['Accept'].eql?(
"application/vnd.railsapibase.v#{@version}"))
end
end
app/controllers/api/v1/api_controller.rb
class Api::V1::ApiController < ApplicationController
include Concerns::Response
include Concerns::ErrorHandler
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。