Ruby on Rails tips

1. Use  Javascript and Jquery with Rails

  • Install jquery rails:
    For this add following code in gam file
    gem 'jquery-rails'
    Install gem (Run on command prompt)
    $bundle install
  • Install jquery package (Run on command prompt)
    $rails generate jquery:install
  • Add following code in layout/application.html.erb
    <%= javascript_include_tag(:defaults) %>

2. Use PostgreSQL with rails

  • Install PostgreSQL database (Run on command prompt)
    $sudo apt-get install postgresql
  • Create rails application with postgresql database (Run on command prompt)
    rails new pg -d postgresql
  • Install bundle
    bundle install

3. Use Pagination with Rails (using will-paginate gem)

  • Add will_paginate gem in gem file
    For this add following code in gam file
    gem 'will_paginate'
  • Install bundle
    $bundle install
  • Use following code instead of Model.all
    @data = Model.paginage(:page => params[:page], :per_page => 10)
  • Include following code in required view
    <%= will_paginate @category %>
  • Remember that all other codes remain same in controller, model and views

4. Rename rails application:

  • First install grep if not installed
    $sudo apt-get install grep
  • Then execute  $grep -ir old_name * and make sure old_name appears only in places you want to replace, then do:
  • $replace old_name new_name -- `grep -r old_name * | cut -d ':' -f 1`
  • Which changes the old_name to new_name in following files
    • config/application.rb
    • config/environment.rb
    • config/environments/development.rb
    • config/environments/production.rb
    • config/environments/test.rb
    • config/initializers/secret_token.rb
    • config/initializers/session_store.rb
    • config/mongoid.yml (if using Mongoid)
    • config/routes.rb
    • config.ru
    • Rakefile
    • app/views/layouts/application.html.erb, in title tag

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s