How to get current_user in model and observer Rails

If you want to log the user who created, updates and delete a object in specific model.There is no default way to fetch the current user in models, current_user is always assigned in controllers (as per authentication plugins, restful_authentication, authlogic and devise). Found one way, by adding some steps in model/user.rb, we can get the […]

ActionView::Template::Error (undefined local variable or method `bootstrap_flash’ in production mode

Create a helper file as,   app/helpers/bootstrap_flash_helper.rb Add these snippet into the file, ——————————————————————————– module BootstrapFlashHelper ALERT_TYPES = [:error, :info, :success, :warning] def bootstrap_flash flash_messages = [] flash.each do |type, message| # Skip empty messages, e.g. for devise messages set to nothing in a locale file. next if message.blank? type = :success if type == :notice […]

How to implement CACHING IN RAILS 3

Why we need to implement? Caching is extremely useful to implement for web applications. When an application is getting a lot of requests and New Relic detects a strain on your instances, it’s probably time to look into caching. Increase performance of the web application. Serving static content to a visitor rather than something generated […]

Serving gzipped assets from Rails application

One good practice for your assets is to gzip them and serve them compressed. To do this in your Rails app, you can make use of the Rack::Deflater middleware. Make sure that you use it in your middleware stack, before the middleware that does the handling of static assets. For example, if you’re using ActionDispatch::Static […]

Shorten URL using Bitly in Rails 3.X

This is achieved by using an HTTP Redirect on a domain name that is short, which links to the web page that has a long URL. This is especially convenient for messaging technologies such as Twitter and Identical which severely limit the number of characters that may be used in a message. Many web developers […]

Scope in model Rails 3.X

Purspose Complex queries in controller make application hard to maintain. Move them into the model as named_scope or scope(Rails 3.X) make the controller simple and the complex find logics are all in models. Suppose we have in controller, class JobsController < ApplicationController     def index         @published_jobs = Job.find(:all, :conditions => { :status […]