Javascript internationalization in Ruby on Rails

Sooner or later you'll face with javascript internationalization if you are developing a multilingual application. There is a wonderful i18n-js gem in Ruby on Rails for that. In spite of existing manuals, it took some time for me to figure out how it works.

Let's localize select2 messages from the example in the previous article.

Localized routes in Rails: tips and tricks

  scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do
    resources :posts
    root to: redirect("/%{locale}/posts", status: 302)
  end
  root to: redirect("/#{I18n.default_locale}", status: 302), as: :redirected_root
  get "/*path", to: redirect("/#{I18n.default_locale}/%{path}", status: 302), constraints: {path: /(?!(#{I18n.available_locales.join("|")})/).*/}, format: false

Let's take a look at the above code sample from config/routes.rb and will study what it does line by line.

Passing data to javascript using gon and jbuilder in Rails

Sometimes it is required to pass some data from server-side code to client-side javascript code.

There is a gon gem for that and it has pretty good manual. Nevertheless, I had some issues using this gem, so I decided to share my experience.

Ruby on Rails admin area scaffolding

Each and every Ruby on Rails website typically has its own admin area. Ryan Bates (a well-known person in Rails world) says in one of his first railscasts that it is a good idea to have the same code base for admin and public area of the website. Another approach could take place, I think. And this is why:

  • usually, admin area has more actions in controllers and more complex data output with variety of fields;
  • public area usually has simpler output;
  • having common code base for admin and public area leads to code complexity and increases the amount of access permission checks.

Thus, it is more sensible to separate admin area from public area. This way public area code will be simpler and cleaner and admin area code will do only what it's meant for. Of course, there are gems, such as activeadminrails_admin, etc. They allow to generate full-featured admin area. But implementation of your own custom admin area sometimes is more suitable, rather than customization of existing complex mechanism.

I'd like to tell you how to separate admin area from public area by example.