How to deploy rails 3.1.1 app on heroku Cedar stack

Posted by Randika on November 09, 2011

Rails 3.1 runs best on Heroku’s Cedar stack, rather than bamboo stack. Following steps worked for me when deploying the apps to heroku’s cedar.

Gem configuration
==============================
1) On your Gem file move the default sqlite3 gem config to development group and add PosgreSQL

group :production do
# gems specifically for Heroku go here
gem "pg"
end

group :development, :test do
gem 'sqlite3'
end

2) Bundle install with this line – (for me just “bundle install” didn’t work on production)
bundle install --without production

Assets configuration
==============================

2) Enable this line on application.rb

# If you want your assets lazily compiled in production, use this line
Bundler.require(:default, :assets, Rails.env)

3) On production.rb set the compile to true
# Don’t fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true

Deploy to Heroku Cedar stack (This is for rails 3.1 apps)
============================================================
heroku create --stack cedar
git push heroku push master

And your app will be here: .herokuapp.com (Note its not heroku.com)

Hope this will help someone

Linux tips: Check whether the mongrel port 3000 is open 1

Posted by Randika on June 03, 2011

Linux tips: How To Search Shell Command History 2

Posted by Randika on June 03, 2011

How to Install Ruby 1.9.2 and Rails 3.0 on Ubuntu 6

Posted by Randika on February 05, 2011

Most of the developers still use package managers to install ruby. This approach works, but most of the times it leads to dependency issues every now and then. Usually I don’t use package managers to install ruby, instead I use ruby version manager (RVM) by wayneeseguin

In this post I’m going to install ruby 1.9.2 on Ubuntu with the help of rvm.

Usually I use “aptitude” to install packages as It’s a better way to track the dependencies. If you don’t have it installed on your system, you can get it by executing these command on a shell prompt.

Great everything is ready for you now. Have fun with rails 3.

How to unzip bz2 files 1

Posted by Randika on June 18, 2010

tar –use-compress-program bzip2 -xvf your-file-name.tar.bz2

Fedora: Accidentally deleted Gnome desktop panels, how to restore? 1

Posted by Randika on June 15, 2010

I accidentally deleted the entire panels at the bottom and the top of the my fedora Gnome installation. Start menu, the clock, desktop switchers, workspaces and everything are gone now.

After googling for few minutes I found a fix for this. Here is what I did.

[randika@localhost ~]$ mkdir bak
[randika@localhost ~]$ mv -rf {.gnome*,.gconf*} bak/

As you can see I moved (I really don’t recommend deleting configuration files) all the gnome configuration files from my account to another directory named

/home/randika/bak/

then I restarted the OS. Done, now I have the default settings back on my screen.

Ruby on Rails: How to bypass Model validation 4

Posted by Randika on April 09, 2010

In my recent application I have these two models (User, BusinessAccount) with some data validation which works well with their respective controllers.

While keeping these models with their data validation as it is, I wanted to create another Controller (SignupController) which save both model data at once. But  I wanted to bypass the original model validation when saving the data from SignupController.

Here is what I did to achieve it with save_with_validation(perform_validation = false) , hope this will help someone else who looking for the same.

JavaScript: trim() Function

Posted by Randika on February 09, 2010

Where is the trim() function to remove leading and trailing whitespaces of strings? Actually Javascript String object doesn’t natively support it. But there is a workaround for this with regular expressions.

Ruby on rails: how to install ruby 1.8.7 on windows 6

Posted by Randika on January 11, 2010

I couldn’t find the very famous ruby one-click-installer for ruby 1.8.7, after trying various ways I ended up with a solution for this. Here is what I did to get ruby 1.8.7 installed on windows.

First download the ruby 1.8.7 from this link: http://www.ruby-lang.org/en/downloads/ (I took Ruby 1.8.7-p72 Binary)

extract this to c:\ruby (or somewhere else you prefer but avoid using spaces on directories for your own safety)

Now update your PATH environment variable with appending c:\ruby\bin and verify the status of the installation by typing ruby -v in the command line prompt.

C:\Users\Randika>ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]

Ok now we need some dependencies packages which doesn’t bundle with ruby 1.8.7

Download libiconv package from here
http://sourceforge.net/projects/gettext/files/
find the libiconv-1.9.1.bin.woe32\bin\iconv.dll and place it inside c:\ruby\bin\

Download zlib package from here
http://www.zlib.net/zlib123-dll.zip
extract it and find the zlib1.dll and place it inside inside c:\ruby\bin\

OK, now we can install rubygems
Get it from here rubygems-1.3.5.zip and extract to wherever you prefer. And then run ruby setup.rb

D:\gems\rubygems-1.3.5>ruby setup.rb

by now gems should be ready to use with. Now you can update gems with

gem update --system

finally install rails

D:\>gem install rails
.....
...
D:\>rails -v
Rails 2.3.5

UPDATE: (20100422)
There is a Release Candidate (RC) of Ruby 1.8.7 (Ruby 1.8.7-p249 (RC2)) at http://rubyinstaller.org/download.htm
You can try that instead of what I have told in this post. Anyway I didn’t try it myself yet.

Most wanted ruby on rails code snippets, tips and tricks 1

Posted by Randika on November 30, 2009

I keep forgetting all those fancy ruby on rails commands and the important code snippets always. I better keep them documented some where I can find easily. So I’m going to write down as much as code snippets, command lines, tips tricks where I need. Anyway this looks messy as I update this page time to time, hope you also can contribute for the list. I hope this will help someone else too.

How to add/remove new columns to table (models) ?
you can specify columns you want to add/remove in your migration by passing attribute:type pairs to the migration generator command line. here is an example to add the mobile number to existing user model.

[randika@localhost MyApp]$ script/generate migration AddMobileToUser mobile:string

and run the rake db:migrate command. This will create the migration schema for you as:

class AddMobileToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :mobile, :string
  end
 
  def self.down
    remove_column :users, :mobile
  end
end

Similarly when you need to remove a column try using below command, followed by the the rake db:migrate

[randika@localhost MyApp]$ script/generate migration RemoveMobileFromUser mobile:string

How to create dynamic drop down menu from a another model

<%= f.select :country_id, Country.all.collect {|country| [ country.name, country.id ] } %>

this will output:

<select id="country_country_id" name="country[country_id]">
<option value="1">Country A</option>
<option value="2">Country B</option>
<option value="3">Country C</option>
</select>



How to find the configuration settings, versions of the development environment

  • To determine the version of Rails that you are running, you can issue rails -v at a command prompt.
  • $rails -v
  • To determine the Rails installation path type which rails at a command prompt. (only on linux)
  • $which rails
  • To determine the list of gems installed type gem list
  • $gem list



How to print the month name from a month number


Putting this code in a view, will print “December” as the output.

<%= Date::MONTHNAMES[12] %>

How to assign a css class to link_to method in Ruby on Rails

How to install/start mongrel http server

$ sudo gem install mongrel
$ cd yourrailsapp
$ mongrel_rails start -d



How to stop mongrel http server

$ mongrel_rails stop