Rails: After installing development routes in the namespace, the model name has a namespace prefix. How to remove it?

I am using an authentication program in my Rails RESTFul API service. Devise allows me to create a new user using [POST] http://domain/users with form_data:

 [user]password = 123 [user]email = foo@bar.zoo [user]password_confirmation = 123 

Then I put devise_for in a namespace like this

 namespace :api do namespace :v1 do devise_for :users, controllers: { :registrations => 'api/v1/registrations', :sessions => 'api/v1/sessions', :passwords => 'api/v1/passwords' } end end 

The controller file structure looks like this.

 . β”œβ”€β”€ api β”‚  └── v1 β”‚  β”œβ”€β”€ passwords_controller.rb β”‚  β”œβ”€β”€ registrations_controller.rb β”‚  └── sessions_controller.rb β”œβ”€β”€ application_controller.rb 

After I made this change, I have to use [POST] http://domain/api/v1/users to create a new user, but with the following form_data p>

 [api_v1_user]password = 123 [api_v1_user]email = foo@bar.zoo [api_v1_user]password_confirmation = 123 

I do not want the model name (that is, the user ) to start with the api_v1 _ prefix. Because if I ever switched my version of api to v2 , then I need to update all my client-side APIs!

Any ideas?

+6
source share
4 answers

On routes you can try the following:

 namespace :api, as: nil do namespace :v1, as: nil do |version| devise_for :users, controllers: { :registrations => "api/#{version}/registrations", :sessions => "api/#{version}/sessions", :passwords => "api/#{version}/passwords" } end end 
+6
source

Check out deva Set up routes doc. You can achieve this by setting up api routes yourself. For instance,

 namespace :api do namespace :v1 do devise_scope :user do resources :sessions, defaults: {format: :json} resources :registrations, defaults: {format: :json} end end end 
+2
source

It looks like you are using custom development controllers, so you should be able to configure the authentication scope in the controller:

 warden.authenticate!(scope: :user) 
0
source

I came across this while trying to use the gem of the Versist version for custom version authentication strategies (Rails 5). Here is my solution to configure it without the need for a special controller:

/config/initializers/inflections.rb gets an inflector, so the API will not display as Api. its pleasant

 ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'API' end 

routes.rb

 api_version(:module => "API::V1",:path => {:value => "api/v1"}) do devise_for :users, :as => :fancy, #removes "api_v1" from the derived model scope, but it cant be blank so pick something that doesn't contain the version. :path => "", #removes "users" from the path so it is just "api/:ver/login" :path_names => { sign_in: 'login', sign_out: 'logout'} #changes the 'action' part of the path name, just flavour. end #if you need a scope, you must now include the scope you choose in devise_for :as. for example: devise_scope :fancy_user do #helper route so we can test with a logout link or url using GET get :logout, :controller => :sessions, :action => :destroy end end 

It will also allow you to add your own authentication strategy and do great with solutions like 4TRABE remote authentication http://4trabes.com/2012/10/31/remote-authentication-with-devise/ Now you can have a nice version of external authentication external API. Of course, we can add a custom controller, but there is no need to interrupt the version number from the area.

Just remember that since you changed the scope, you will need to reference it this way when you register it with the boss. i.e. The /devise.rb initializers end up like this:

 config.warden do |manager| manager.strategies.add(:remote_authenticatable, Devise::Strategies::RemoteAuthenticatable) manager.default_strategies(:scope => :fancy_user).unshift :remote_authenticatable end config.add_module :remote_authenticatable, :controller => :sessions, :route => { :session => :routes } 

Hope this helps someone enjoy ^ _ ^

0
source

Source: https://habr.com/ru/post/976098/


All Articles