Rails Filters: Before, After, and Around Filters

Cloud Computing | #CloudFoundry | Ruby | Go | OpenStack | Ansible | Docker | Jenkins

Filters are methods that are run before, after or “around” a controller action.

Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.

Before filters may halt the request cycle. A common before filter is one which requires that a user is logged in for an action to be run.

Before filters are run on requests before the request gets to the controller’s action. It can return a response itself and completely bypass the action.

The most common use of before filters is validating a user’s authentication before granting them access to the action designated to handle their request. I’ve also seen them used to load a resource from the database, check permissions on a resource, or manage redirects under other circumstances.

After filters are run after the action completes. It can modify the response. Most of the…

View original post 115 more words

Caching in Rails 4

For caching based on ActiveRecord array i.e. single key for all restaurants

Your helper function which is available throughout the app

module RestaurantsHelper
def restaurant_promotion_key # to use this check file pages/home.html.erb
count = Restaurant.order_by_fee_plans.count
max_updated_at = Restaurant.order_by_fee_plans.maximum(:updated_at).try(:utc).try(:to_s, :number)
“restaurants/restaurant_promotion-#{count}-#{max_updated_at}”
end
def all_restaurant_key # to use this check file shared/_header_banner.html.erb
count = Restaurant.count
max_updated_at = Restaurant.maximum(:updated_at).try(:utc).try(:to_s, :number)
“restaurants/all-#{count}-#{max_updated_at}”
end
end

Using this in your view

<% cache(all_restaurant_key) do %>

<!– Your UI logic goes here–>

<% end %>

For caching for single row in ActiveRecord

<% cache(restaurant) do %>

<!–Your UI code on ActiveRecord: restaurant goes here–>

<% end %>

The key will be made by by rails and maintained automatically by Rails

Running local server as production server

http://stackoverflow.com/questions/10098743/rails-3-1-3-trying-to-run-locally-in-production-asset-pipeline-not-working

Rails doesn’t serve static assets in production mode by default. If you want to disable this and serve the static assets, update your config/environments/production.rb file with this:

config.serve_static_assets = true

The reasoning behind Rails’ default configuration is that it assumes you’ll be running behind a standard web server in production mode (Apache, Nginx, etc.) which will then proxy the requests to your Rails app. Since the assets are precompiled, Apache/Nginx/etc. can directly serve them without needing to talk to your Rails processes.

Precompile your assets on local server using the below cmd from app

$>rake assets:precompile RAILS_ENV=production

Then edit your config/environments/roductions.rb and set the below flag to true, so that rails server sends assets from server

config.serve_static_assets = false

then start your local server as production

$>rails server -e production

Post your usage u can remove the precompiled assets from the local env using

     $>rake assets:clean