Search code examples
ruby-on-railsrubyroutesdeviseconstraints

devise and language by multiple domain


I'm trying to use multi language by domain, for example I have domain.fi.local for language Finnish and domain.es.local for language Spanish and on every of them you can switch to English and I have to add constranits per domain but this disables devise on each domain, I have used devise_for outside the constraints because if I add devise_for in each domain constraints I get the error for calling two times devise_for . Here is my routes.rb.

Rails.application.routes.draw do
  devise_for :users

  constraints(host: "domain.fi.local") do
    scope "(:locale)", locale: /fi|en/ do
      resources :users do
        resource :profile, only: [:show, :edit, :update]
      end
      resources :trips
    end
  end

  constraints(host: "domain.es.local") do
    scope "(:locale)", locale: /es|en/ do
      resources :users do
        resource :profile, only: [:show, :edit, :update]
      end
      resources :trips
    end
  end

  # General fallback routes (English default)
  scope "(:locale)", locale: /en|fi|es/ do
    resources :users do
      resource :profile, only: [:show, :edit, :update]
    end
    resources :trips
  end

  # Health check
  get "up" => "rails/health#show", as: :rails_health_check

  # Unique root path to avoid conflicts
  root "trips#index"
end

But when I'm navigating to a devise route I get this error:

Error accesing devise route for logout

I tried to put devise inside the constraints but I get the same error.

Rails.application.routes.draw do
  # Define Devise routes globally (outside constraints)
  devise_for :users

  constraints(host: "domain.fi.local") do
    scope "(:locale)", locale: /fi|en/ do
      devise_for :users, skip: :all  # Prevent duplicate routes inside constraints
      resources :users do
        resource :profile, only: [:show, :edit, :update]
      end
      resources :trips
    end
  end

  constraints(host: "domain.es.local") do
    scope "(:locale)", locale: /es|en/ do
      devise_for :users, skip: :all  # Prevent duplicate routes inside constraints
      resources :users do
        resource :profile, only: [:show, :edit, :update]
      end
      resources :trips
    end
  end

  # General fallback routes (English default)
  scope "(:locale)", locale: /en|fi|es/ do
    devise_for :users, skip: :all  # Prevent duplicate routes
    resources :users do
      resource :profile, only: [:show, :edit, :update]
    end
    resources :trips
  end

  # Health check
  get "up" => "rails/health#show", as: :rails_health_check

  # Unique root path to avoid conflicts
  root "trips#index"
end


Solution

  • It was because of the sign_out link was using get instead of delete

      devise_for :users, controllers: { sessions: 'users/sessions' }, sign_out_via: [:delete, :get]
    

    Will fix the isue.