当两个路由指向同一模型时,使用Devise resource_name发出问题

时间:2016-01-13 20:31:13

标签: ruby-on-rails api devise routes

我的api v1有两条路线,v2指向同一条Devise模型,路线如下:

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中的任何一个时,Devise resource_name是' v1_user'或者' v2_user',但发送的参数具有params [' user']所需的值。

我从Devise helpers.rb devise_parameter_sanitizer方法中获取此信息。当Devise :: ParameterSanitizer.new(resource_class,resource_name,params)params如下所示:

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"

不确定这是否有用,但这里是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

1 个答案:

答案 0 :(得分:1)

我最终提出了一个解决方案,但并不是我喜欢的那种解决方案。我仍然对其他建议持开放态度,所以我不打算将此标记为已接受的答案,但它至少完成了工作。

基本上在我的设计控制器中,我添加了一个before_filter,它将期望的参数添加到request.parameters和local params中。它非常简单,但感觉非常酷。当然,我的单位测试了它的地狱,因为将来会改变它。

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