Using arrays of hstore with Rails 4

Rails 4 Json data-type and forms to use.

Advertisements

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


Rails Authentication redirect back after authentication

Plz, go to this url….

Rails Authentication redirect back after authentication

 

 

I was in the same situation:

If you are like me,and Ryan Bates, you like doing the important things yourself.  If there are errors, they are your errors.
Chances are you will want to create your Rails authentication from scratch yourself. Ryan of Railscasts has an excellent screencast about this.

I switched from Devise to the  custom authentication using this screencast. It was a breeze.
So now comes the customisation. One of these is the all-important Rails authentication redirect back to the funnel a user came from after they are done signing in, or up.

This can actually be achieved very easily:

Force authentication

Force authentication

Say I have a CharitiesController, that I want to close to unauthenticated users. In line with Devise, I would do this to force them to authenticate:

Set a return point

In my ApplicationController, given i have set the user sign in route to user_login, I can define a return point: