Rails : i18n : unexpected redirections

In my app, I have different User types like VehicleOwner , GarageOwner, Admin and unidentified/unauthenticated. I wanted to implement localization for English and Finnish. I was successful, but suddenly all my old URLs stopped working; when I tried to access the old URLs I was redirected to login page, and the login page was also not loading – stuck on infinite redirection loop.

Continue reading

Rails | Devise | Internationalization I18n

If you are upgrading your app from uni-lingual to bilingual, you might want to have separate pages for Devise related pages as well. Its simple for simpler implementation of devise like no ‘omniauth’. But if you are including ‘omniauth’ then normal recommended approach will not work.

# Before
devise_for :users, path: '',
  path_names: { sign_up: :signup, sign_in: :login, sign_out: :logout },
  controllers: {
    registrations:      'devise_override/registrations',
    confirmations:      'devise_override/confirmations',
    sessions:           'devise_override/sessions',
    omniauth_callbacks: 'vehicle_owners/omniauth_callbacks'
# After
devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'vehicle_owners/omniauth_callbacks'}
scope "(:locale)", locale: /en|fi/ do
  devise_for :users, path: '',
             path_names:   { sign_up: :signup, sign_in: :login, sign_out: :logout },
             controllers:  {
                 registrations:      'devise_override/registrations',
                 confirmations:      'devise_override/confirmations',
                 sessions:           'devise_override/sessions'
             }, skip: :omniauth_callbacks

So, it turns out that you cannot scope the Omniauth related URLs, as it makes no sense. You might also see warnings like

Devise does not support scoping OmniAuth callbacks under a dynamic segment
and you have set "/(:locale)/". You can work around by passing
`skip: :omniauth_callbacks` to the `devise_for` call and extract omniauth
options to another `devise_for` call outside the scope. Here is an example:

 devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}

 scope '/(:locale)', locale: /ru|en/ do
   devise_for :users, skip: :omniauth_callbacks

Note: Do not forget to restart the application after any major changes.

Its easy to determine URL in Rails

Rails has made so much easy to generate URL for RESTful resources. You don’t have to write the long route helper method; there exits a short hand method to be used; like

url_for [:edit, @parent_object, @resource_object]


When using book_article_path, you can just pass in instances of Book and Article instead of the numeric IDs:

<%= link_to 'Article details', book_article_path(@book, @article) %>

Moreover, you can use url_for with a set of objects, and Rails will automatically determine which route you want to generate:

<%= link_to 'Ad details', url_for([@book, @article]) %>

If you want any specific :action then try

<%= link_to 'Ad details', url_for([:edit, @book, @article]) %>

Some More

If you wanted to link to just a book:

<%= link_to 'Book details', @book %>

If you want to link to nested resource then

<%= link_to 'Article details', [@book, @article] %>

Note: the above technique will only work with link_to helper method.



Some useful links


Devise: Resource specific Logic in Rails Routes : Examples

If you are willing to implement or learn how to route a particular URL request to specific controller on the basis of which role the currently logged-in user is.

# in routes.rb
authenticate :user, -> (user) { user.stylist? } do
  resources :stylist_profiles, only: [:index, :show], path: '/profile'

authenticate :user, -> (user) { user.customer? } do
  resources :customer_profiles, only: [:index, :show], path: '/profile'

The URL /profile or /profile/1 will route to StylistProfileController if user’s role is Stylist and will route to CustomerProfileController if user’s role is Customer .

condition = -> (u) {
  ['qa', 'development', 'staging'].include?(Rails.env) || (u.admin? && Rails.env.production?)

authenticate :user, condition do
  mount Sidekiq::Web => '/sidekiq'
  mount RedisBrowser::Web => '/redis-browser'

Some additional examples

# in config/routes.rb
# Home page is root for UnAuthenticated visitors
#  This shall be at the top
unauthenticated :user do
  root 'pages#home', page: 'home'

authenticate :user, ->(user) {user.vehicle_owner? } do
  root 'vehicle_owners/quote_requests#index'

authenticate :user, ->(user) {user.garage_owner? } do
  root 'garage_owners/quote_requests#index'

Rails Deeplinking to inner pages of the app

What is deep linking?

App deep linking is a fancy way to refer to a URL that points to a specific part of an app. It’s analogous to a URL that points to a subfolder of a website. For example if I wanted to send you an article on Rails Developer Community, I would send you a URL that immediately brought you to the article, not send you to the shivabhusal.com main portal to navigate to it yourself from there. Continue reading

Rails : Routes : Difference between resource and resources in routes.rb

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

Continue reading