Example application to show issue with using devise and route_translator in the same rails project.
Here are all the steps to recreate this example application from scratch, assuming Ruby and Rails are installed:
$ rails new drt-example --minimal
$ cd drt-example
$ rails db:create
This step is optional, but useful to verify that the route translation setup is working properly.
Create a new controller:
$ rails g controller home index example
Set up the views with some content:
app/views/home/index.html.erb
<h1><%= t(:greetings) %></h1>
<%= link_to t(:example), action: :example %>
app/views/home/example.html.erb
<%= I18n.locale %>
Add route_translator
to the Gemfile.
Set up localized routes in config/routes.rb
:
Rails.application.routes.draw do
localized do
root to: 'home#index'
get 'example', to: 'home#example'
end
end
Set up locales:
config/locales/en.yml
en:
greetings: Welcome
example: Example
config/locales/de.yml
de:
greetings: Willkommen
example: Beispiel
routes:
example: beispiel
Make sure locale is set for controller/view in app/controllers/application_controller.rb
:
class ApplicationController < ActionController::Base
around_action :set_locale_from_url
end
At this point, everything works as expected.
Add devise
to the Gemfile.
Set up a user class with devise.
$ rails generate devise:install
create config/initializers/devise.rb
create config/locales/devise.en.yml
$ rails generate devise user
invoke active_record
create db/migrate/20210206133737_devise_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
insert app/models/user.rb
route devise_for :users
$ rails db:migrate
== 20210206133737 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-> 0.0018s
-- add_index(:users, :email, {:unique=>true})
-> 0.0006s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0006s
== 20210206133737 DeviseCreateUsers: migrated (0.0031s) =======================
Add before_action :authenticate_user!
to app/controllers/application_controller.rb
.
Move the devise routes inside the localized block in config/routes.rb
(this is the step that breaks things).
Accessing /de
without being logged in should redirect to /de/users/sign_in
, but redirects to /users/sign_in
.
This will result in the login for being in the wrong locale. Moreover, in more complex situations, like host based routing, this will break completely (most likely showing a 404 or 500 error page instead of the log in form).