Sunday, October 30, 2011

Upgrade Ruby version which is installed with RVM


  • Get latest Ruby version for RVM
    • rvm get latest
  • Find out current Ruby version
    • rvm current (the string between first '-' and '@')
  • Find out new Ruby version
    • rvm list known
  • Upgrade
    • rvm upgrade [current version] [new version]

Saturday, October 29, 2011

[Rails] Redirect to previous page

In controller, you can specify redirection to go back to previous page that call to this controller by using code below:
redirecto_to :back  
Note: Rails 3.1

[Rails] Route alias

You can execute "rake routes" to view display routes in your app.

The first column of output are route aliases which you can refer to in your app or test by append "_path" to that alias.

If you use resource in routes.rb, it will automatically generate CRUD routes and their alias for you.

But if you want to alias your other routes, you can do by append below after your routes
, :as => :{alias you want}
Note: Rails 3.1

Friday, October 28, 2011

[Heroku] Access Rails's model in migration, heroku rake db:migrate, uninitialized constant

I wanted to use Rails's migration to add new column to a table and fill this column with default value.
add_column :students, :student_id, :integer 
Student.all.each { |s| s.update_attributes!(:student_id => s.id+10000) }
I run this locally.
rake db:migrate
It worked without any problem.

Then I deployed to Heroku and run;
heroku rake db:migrate
I found this error.
 uninitialized constant AddStudentIdToStudents::Student
After googling for a while, I found this blog post Heroku Migration Fails to Update Attributes: reset_column_information

The way to solve this issue(don't know why) is to open up the class inside migration file like this;
class AddStudentIdToStudents < ActiveRecord::Migration
  class Student < ActiveRecord::Base; end
  def change
    add_column :students, :student_id, :integer
    Student.all.each { |s| s.update_attributes!(:student_id => s.id+10000) }
  end
end
This works for me.

Note: Rails 3.1.0 

Sunday, October 16, 2011

My Note from "The Intro to Rails Screencast I Wish I Had"

I found that The Intro to Rails Screencast I Wish I Had is very good in demostrating some tricks that make Rails different to other web development frameworks.

Below are notes from my 2nd time watching this screencast. What I note are things that I've never known before or I can't really remember the way to use it.

  • Add -T in rails new to prevent rails from generating Test::Unit
  • Gems
    • turn (Pretty printing for test results)
    • guard-rspec (Autotest)
    • growl_notify
    • rb-fsevent
    • launchy
      • save_and_open_page (use along with Capybara statements)
  • Guard
    • guard init rspec
    • guard (start guard)
  • RSpec integration test
    • rails g integration_test {name}
  • Routes
    • rake routes
    • resources (auto routes)
  • Capybara (route_name comes from first column of rake route)
    • visit {route_name}_path
    • page.should have_content {string}
    • fill_in {id/name/label}, :with => {string}
    • click_button {id/name/label}
    • current_path.should == {route_name}_path
    • find_field('').value
  • ActiveModel
    • {Model}.create {value}
    • {model_instance}.update_attributes {fields hash}
  • ERB
    • form_for {object} do | f |
    • f.label :{name}
    • f.text_field :{name}
    • f.submit
    • render '{partial view(No _)}'  <- partial view file name must prefix with _ 
    • flash.each do | name, message | (come from params passed with redirect_to)
  • Controller
    • render :text => params.inspect
    • redirect_to :back (previous page)
    • (redirect_to) {:notice | :alert} => {string} (will be available in flash)


Saturday, October 8, 2011

Make Rails accept Non-English Characters

Put following line to the first line of the page that contain Non-English characters
# encoding: utf-8

Config number of file descriptors on Linux

Check file descriptor limit for a process. Vary depending on owner of the process $ cat /proc/{process-id}/limits Check current number of...