问题与设计,当两个路线点RESOURCE_NAME到相同型号 [英] Issue with Devise resource_name when two route point to the same model

查看:175
本文介绍了问题与设计,当两个路线点RESOURCE_NAME到相同型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个途径对我的API v1和v2指向相同的设计模型下面的路线:

 范围:API做
  命名空间:v1的做
    devise_for:用户:控制器=> {:注册=> 色器件/ V1 / users_registrations
                                      :会话=> 色器件/ V1 / users_sessions
                                      :密码=> 色器件/ V1 / users_passwords}
  结束
结束适用范围:API做
  命名空间:2版做
    devise_for:用户:控制器=> {:注册=> 色器件/ V2 / users_registrations
                                      :会话=> 色器件/ V2 / users_sessions
                                      :密码=> 色器件/ V2 / users_passwords}
  结束
结束

我的问题是,当一个请求被发送到这些API之一是该设计RESOURCE_NAME要么是'v1_user'或'v2_user',但在发送的参数有params中['用户']所需要的值。

我从设计helpers.rb devise_parameter_sanitizer方法此信息。当设计:: ParameterSanitizer.new(resource_class的,RESOURCE_NAME,则params)PARAMS看起来如下:

 结果= {ActionController的参数:: ActionController的} ::参数(4元)
'用户'= {ActionController的参数:: ActionController的} ::参数(16元)
 电子邮件=test@kemsoft.co
 密码=QWERTYUIOP
 'FIRST_NAME'=测试
 '姓氏'=TESTTEST
 ship_street_address'=1主ST
 ship_city'=罗马
 ship_state'=TX
 ship_zip_ code'=12345
 dealer_number'=1234567890
 BUSINESS_NAME=ABC泵
 bus_street_address'=121西大街
 bus_city'=圣安东尼奥
 bus_state'=NY
 bus_zip_ code'=73412
 'distributor_id'=1
 'additional_distributor_id'=1
控制器=色器件/ V1 / users_registrations
动作=创造
格式化=JSON

不知道这将是有用的信息,但这里是devise_mapper:

 结果= {设计::映射}#<制定::映射:0x007fc7a2c8d950>
@class_name =用户
@Controllers = HASH(3片)
@failure_app = {}类设计:: FailureApp
@format =零
@klass = {设计::消气}#<制定::消气:0x007fc7a2c8d338>
@modules =阵列(7元)
@path =用户
@path_names =散列(8元)
  注册= GT;
  新= GT;新
  编辑= GT;编辑
  sign_in =>签到
  SIGN_OUT =>登出
  密码=>密码
  sign_up =>注册
  取消= GT;取消
@ path_ preFIX =/ API / V1
@router_name =零
@routes =阵列(3片)
  [0] = {}符号会
  [1] = {}符号密码
  [2] = {}符号注册
@scoped_pa​​th =V1 /用户
@sign_out_via = {}符号删除
@singular = {}符号v1_user
@strategies =阵列(2元)
  [0] = {}符号rememberable
  [1] = {}符号database_authenticatable
@used_helpers =阵列(3片)
  [0] = {}符号会
  [1] = {}符号密码
  [2] = {}符号注册
@used_routes =阵列(3片)
  [0] = {}符号会
  [1] = {}符号密码
  [2] = {}符号注册


解决方案

我最终想出一个解决方案,但它不是一个我喜欢所有的东西。我仍然开放给其他建议,所以我不会以纪念这是公认的答案,但它ATLEAST了这项工作。

基本上在我的色器件控制器,我补充说,增加的预期参数来request.parameters和当地PARAMS一个的before_filter。这是pretty简单,但感觉真的哈克。当然,我已经进行单元测试的地狱它柜面变化在未来打破它。

 的before_filter:sanitize_params高清sanitize_params
  request.parameters [:v2_user] =参数[:用户]除非PARAMS [:用户] .nil?
  除非PARAMS [:用户] .nil PARAMS [:v2_user] =参数[用户]
结束

I have two route for my api v1 and v2 pointing to the same Devise model with the below routes:

scope :api do
  namespace :v1 do
    devise_for :users, :controllers => {:registrations => "devise/v1/users_registrations",
                                      :sessions => "devise/v1/users_sessions",
                                      :passwords => "devise/v1/users_passwords"}
  end
end

scope :api do
  namespace :v2 do
    devise_for :users, :controllers => {:registrations => "devise/v2/users_registrations",
                                      :sessions => "devise/v2/users_sessions",
                                      :passwords => "devise/v2/users_passwords"}
  end
end

My problem is when a request is sent to either one of these API's the Devise resource_name is either 'v1_user' or 'v2_user', but the parameters sent in have the values needed in params['user'].

I get this information from the Devise helpers.rb devise_parameter_sanitizer method. When Devise::ParameterSanitizer.new(resource_class, resource_name, params) params looks like below:

result = {ActionController::Parameters} ActionController::Parameters (4 elements)
'user' = {ActionController::Parameters} ActionController::Parameters (16 elements)
 'email' = "test@kemsoft.co"
 'password' = "qwertyuiop"
 'first_name' = "test"
 'last_name' = "testtest"
 'ship_street_address' = "1 main st"
 'ship_city' = "rome"
 'ship_state' = "TX"
 'ship_zip_code' = "12345"
 'dealer_number' = "1234567890"
 'business_name' = "ABC Pumps"
 'bus_street_address' = "121 West Ave"
 'bus_city' = "San Antonio"
 'bus_state' = "NY"
 'bus_zip_code' = "73412"
 'distributor_id' = "1"
 'additional_distributor_id' = "1"
'controller' = "devise/v1/users_registrations"
'action' = "create"
'format' = "json"

Not sure if this will be helpful information, but here is the devise_mapper:

result = {Devise::Mapping} #<Devise::Mapping:0x007fc7a2c8d950>
@class_name = "User"
@controllers = Hash (3 elements)
@failure_app = {Class} Devise::FailureApp
@format = nil
@klass = {Devise::Getter} #<Devise::Getter:0x007fc7a2c8d338>
@modules = Array (7 elements)
@path = "users"
@path_names = Hash (8 elements)
  registration => 
  new => new
  edit => edit
  sign_in => sign_in
  sign_out => sign_out
  password => password
  sign_up => sign_up
  cancel => cancel
@path_prefix = "/api/v1"
@router_name = nil
@routes = Array (3 elements)
  [0] = {Symbol} session
  [1] = {Symbol} password
  [2] = {Symbol} registration
@scoped_path = "v1/users"
@sign_out_via = {Symbol} delete
@singular = {Symbol} v1_user
@strategies = Array (2 elements)
  [0] = {Symbol} rememberable
  [1] = {Symbol} database_authenticatable
@used_helpers = Array (3 elements)
  [0] = {Symbol} session
  [1] = {Symbol} password
  [2] = {Symbol} registration
@used_routes = Array (3 elements)
  [0] = {Symbol} session
  [1] = {Symbol} password
  [2] = {Symbol} registration

解决方案

I did end up coming up with a solution, but it's not one I like all that much. I'm still open to other suggestion so I'm not going to mark this as the accepted answer, but it atleast got the job done.

Basically in my devise controllers I added a before_filter that added the expected parameter to the request.parameters and local params. It's pretty simple, but feels really hacky. Of course I've unit tested the hell out of it incase changes break it in the future.

before_filter :sanitize_params

def sanitize_params
  request.parameters[:v2_user] = params[:user] unless params[:user].nil?
  params[:v2_user] = params[:user] unless params[:user].nil?
end

这篇关于问题与设计,当两个路线点RESOURCE_NAME到相同型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆