Importing CSV and Excel in rails 4 and ruby 2.0

You can use a single gem ‘roo’ for reading all types of file, instead of rubyXL, spreadsheet etc. multiple gems for different files(.xls, .xlsx). You can check here railscasts for importing csv and excel import in ruby 1.9.3. But this code snippet is not working with Ruby 2.0. As this episode used gem ‘roo’ for reading files and gem ‘roo’ has been updated…

Things which Rails could do but I didn’t know.

Run Helper methods in the console $rails c => helper.number_to_currency(100) => “$100.00” => helper.time_ago_in_words(3.days.ago) => “3 days” Shorthand Migrations $ rails g resource person first_name:string last_name:string email:string token:string You can write above command as- $ rails g resource person first_name last_name email token Both will generate same migration: class CreatePeople < ActiveRecord::Migration def change create_table…

Send email from application via postmark

Postmark is widely used email delivery service for the web application. The “Postmark-Rails” Gem is a drop-in plug-in for ActionMailer to send emails via Postmark. Links: Postmark-app , Postmark-rails gem , Postmark Developer’s Blog To integrate it with your application: 1. Create an account on postmark.com (it also provides free account), set your-email as sender’s signature. This email-signature only you…

Avoid rails helper times_ago_in_words()

Let’s say we have 100 records in lessons table then %td.last_modified= times_ago_in_words(lesson.updated_at) server calculate the time ago for each record for each request, it wastes a lot of time on server side. If we replace above code by %td.last_modified”= lesson.updated_at.to_s and load jquery plugin  <script src=”jquery.timeago.js” type=”text/javascript”></script> then in JavaScript $(“td.last_modified”).timeago() thats it. Then the…

Reload or Add routes at runtime in Rails 3

In one of my project, we needed SEO friendly Menu urls like /brandy/cognac. For this, we added a generic route/:parent/:child/:grandchild at the bottom of routes.rb. With this approach, there was an issue with invalid urls like /this/is/wrong being redirected to specified controller action. To fix this, we used Menu names to generate specific urls. But the routes would become invalid if Admin…

jquery ketchup-plugin for Client-side form-validation:

jquery ketchup-plugin for Client-side form-validation: There are many jquery plugins available for client-side form validation (to validate form before actually submitting it). jquery ketchup-plugin is one of them. Github: https://github.com/mustardamus/ketchup-plugin Demo: http://demos.usejquery.com/ketchup-plugin/ Validations provided by plugin: https://github.com/mustardamus/ketchup-plugin/blob/master/jquery.ketchup.validations.js Steps: 1. include css file in your application.css file *= require jquery.ketchup 2. include js file in your…