Created
June 29, 2017 17:40
-
-
Save Genkilabs/587fab4214798c87c3aa97bb06280f43 to your computer and use it in GitHub Desktop.
How to use Devise inside of a Versionist scope without the version-name in the model's path prefix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/config/initializers/inflections.rb | |
# This is a nice-to-have so API doesn't keep showing up as Api | |
ActiveSupport::Inflector.inflections(:en) do |inflect| | |
inflect.acronym 'API' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/config/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" | |
# Here is where we add a custom, versioned, controller if we want one. It is not necessary though. | |
# :controllers => { :sessions => "api/v1/sessions" }, #routes our session to our custom controller | |
: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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there, thanks for providing the snippet. However, I am getting an
uninitialized constant
issue, have you encounter this issue before?I did not create any custom controllers for devise. My own controllers work fine living under /api/v1.
Thanks in advance.
config/routes.rb
** $ rails routes **