Last Updated: February 25, 2016
·
1.53K
· kalashnikovisme

Some differences between development and production in Rails

Experienced ROR developers know about differences between development and production. I've described some of them here, which I didn't know before:

#config/routes.rb
resources :pages do
  get ":slug" => "pages#show"
end

#app/controllers/pages_controller.rb
def show
  @page = Page.find_by_slug params[:slug]
end

In development this routes construction will give you params:

{"controller"=>"web/pages", "action"=>"show", "slug"=>"slug"}

But in production it will give you:

{"controller"=>"web/pages", "action"=>"show", "id"=>"slug"}

Value "slug" has key "id", not "slug". As a result the same code gives us different results in different environments. To solve this problem you need to write:

resources :pages do
  collection do
    get ":slug" => "pages#show"
  end
end

Next example is about decorators. If you use gem draper in your project. You need decorator in controller: Admin::UsersController

class Admin::UsersController < Admin::ApplicationController
  def index
    @users = UserDecorator.decorate_collection User.all
  end
end

In Rails 3.2.17 in development UserDecorator is the Admin::UserDecorator, because you use in Admin controller. Ok. You add functions to Admin::UserDecorator without remorse.

But in production UserDecorator is the UserDecorator (!!!), than in production you have error undefinedmethod somefunction in UserDecorator.

If you want to use UserDecorator class without Admin::UserDecorator you should write ::UserDecorator in Admin controller. It will be UserDecorator in development and production.

class Admin::UsersController < Admin::ApplicationController
  def index
    @users = ::UserDecorator.decorate_collection User.all
  end
end

Remember about differences between environments.