本文介绍了当两条路由指向同一模型时,设计资源名称出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 api v1 和 v2 有两条路线指向具有以下路线的相同设计模型:

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

我的问题是,当向这些 API 之一发送请求时,设计资源名称是v1_user"或v2_user",但发送的参数具有 params['user'] 中所需的值.

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'].

我从 Devise helpers.rb devise_parameter_sanitizer 方法中获得了这些信息.当 Devise::ParameterSanitizer.new(resource_class, resource_name, params) params 如下所示:

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' = "[email protected]"
 '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"

不确定这是否是有用的信息,但这里是 devise_mapper:

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.

基本上在我的设计控制器中,我添加了一个 before_filter,将预期的参数添加到 request.parameters 和本地参数.这很简单,但感觉真的很hacky.当然,我已经对它进行了单元测试,以防将来更改会破坏它.

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

这篇关于当两条路由指向同一模型时,设计资源名称出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:28