Postgres Error On Heroku

Recently, I ran into an error while deploying an application(redmine to be specific) on heroku.
I followed all steps listed on the wiki page, remove the required files from gitignore etc but when I did git push, I kept on getting the error:

Please configure your config/database.yml first
remote: rake aborted!
remote: URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname

Since Heroku generates database.yml file itself, I had no clue what to do.
So in the gemfile, I deleted the following block:
# database_file = File.join(File.dirname(__FILE__), "config/database.yml")
# if File.exist?(database_file)
# database_config = YAML::load(ERB.new(IO.read(database_file)).result)
# adapters = database_config.values.map {|c| c['adapter']}.compact.uniq
# if adapters.any?
# adapters.each do |adapter|
# case adapter
# when 'mysql2'
# gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw, :x64_mingw]
# gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
# when 'mysql'
# gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
# when /postgresql/
# gem "pg", "~> 0.17.1", :platforms => [:mri, :mingw, :x64_mingw]
# gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
# when /sqlite3/
# gem "sqlite3", :platforms => [:mri, :mingw, :x64_mingw]
# gem "jdbc-sqlite3", "< 3.8", :platforms => :jruby
# gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
# when /sqlserver/
# gem "tiny_tds", "~> 0.6.2", :platforms => [:mri, :mingw, :x64_mingw]
# gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw, :x64_mingw]
# else
# warn("Unknown database adapter `#{adapter}` found in config/database.yml, use Gemfile.local to load your own database gems")
# end
# end
# else
# warn("No adapter found in config/database.yml, please configure it first")
# end
# else
# warn("Please configure your config/database.yml first")
# end

and added

group :production do
# gems specifically for Heroku go here
gem "pg", ">= 0.11.0"
end

Also in config/environments/production.rb,I added the following line:

config.assets.initialize_on_precompile = false

Once I pushed, the above error went away but I got the following error:


Running: rake assets:precompile
remote: rake aborted!
remote: PG::ConnectionBad: could not connect to server: Connection refused
remote: Is the server running on host "127.0.0.1" and accepting

The problem here was I didn’t pre-provision my database before pushing to heroku.

So solve this, one can run the command:

heroku addons:create heroku-postgresql

So know about the issue, one can always use:

heroku config

Before running the command, heroku config, returned DATABASE_URL as blank and that was the issue.

Hope it helps !!

Resetting a Database in Heroku application

Recently I faced an issue when  I wanted to delete an existing database in an application deployed in heroku and create a new one.

I tried using

heroku run rake db:drop

 

I got an error saying “USER does not have connect privilidges”

The way to solve it is

heroku pg:psql

This logs in to your database and then type:

drop schema public cascade;
create schema public;

After this exit the prompt and type:

heroku rake db:migrate:reset

Hope it helps.

 

Install apache web server and passenger on Ubuntu 11.04(Natty)

I just Installed apache and passenger on Ubuntu 11.04 to run and deploy my ruby on rails applications Passenger is a gem and can work with apache as well as nginx…

The reason for choosing apache is that its an industry standard.

Also one thing I did with apache was created virtual hosts and ran my apps on local machine with domains like http://www.application1.com

In this post I will list the steps I followed in order to setup apache web server and passenger.

Here is my stack:

  •  Ubuntu 11.04
  •  ruby 1.9.2 via rvm
  •  rubygems 1.8.10

I will be using rails 3.1.10(lastest this morning).!!

So lets start !!

  • Install rvm

To install rvm just type the following in the terminal(ctrl+alt+t)

 user$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

You can find more information about rvm on Here

  • Install ruby via rvm

Once rvm is installed you can install the latest or desired version of ruby by typing the following in terminal

rvm install 1.9.2

This will install ruby 1.9.2 on your system.

More information on how to make it your default ruby is available on here

  • Install rails.

just need to do

gem install rails

  • Install passenger

just do

gem install passenger

  • Install apache web server.

To install apache web server type:

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
  • Install passenger apache module.

Once this is done, we must install passenger , an apache module thats hepls us to rub rails apps on apache.

sudo passenger-install-apache2-module
  • Configure.

Finally everything is done.

When you run the passenger apache module, there would be some instructions that installer will give you.

The last thing it will tell is to paste some text in apache configuration file

The configuration file is located at /etc/apache2/apache2.conf

Now once this is done, we are ready to deploy.

This has a lot of information about deploying.

In my next post, I will show how to start rails apps on local machine with apache.

Hope it helps.!

Segmentation Fault while doing bundle install

I recently encountered this error of segmentation fault while doing bundle install in one of my rails application.

After a bit of googling I found out its an issue with ruby 1.9.3.

Here is the solution:

 

rvm pkg install iconv
 rvm pkg install openssl
 rvm reinstall 1.9.3 --with-openssl-dir=~/.rvm/usr --with-iconv-dir=~/.rvm/usr

 

Rails and attr_*

There is a small gotcha in rails attr_accessible method in ruby.

  • If there are no attr_accessible attributes on a model it’s open, any attribute can be written with .update_attributes() or a similar mass-assignment function.
  • If there is even one attr_accessible the model is closed except for those attributes defined to be attr_accessible.

So there is an implicit toggle involved in attr_accessible as well as the actual desired functionality of allowing an attribute to be mass-assigned

Creating virtual hosts in apache in local machine

In this post I show how to create virtual hosts on apache and run multiple rails apps on your local machine with names like

rails_app1.com

rails_app2.com

In the previous post I had shown how to deploy rails apps on local machine with apache web server

My software stack:

  • Ubuntu 11.04
  • ruby 1.9.2 p290.
  • Rails 3.1.10
  • rubygems 1.8.10

Lets start!

First we have to enable virtual aliases on the Apache S

sudo a2enmod vhost_alias

Now I assume that the rails app is at the location:

/home/username/Desktop/rails_apps/my_rails_app

 

We first create a virtual configuration file:

Tyep the following in terminal:

sudo gedit /etc/apache2/sites-available/mydomain

Copy the following settings to your file:

 

<VirtualHost *:80>
ServerAdmin webmaster@domain.com
ServerName my_rails_app.com
ServerAlias http://www.my_rails_app.com

DocumentRoot /home/username/Desktop/rails_apps/my_rails_app/public
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory //home/username/Desktop/rails_apps/my_rails_app/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory “/usr/lib/cgi-bin”>

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

</Directory>

 

ErrorLog ${APACHE_LOG_DIR}/error.log

 

# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn

 

CustomLog ${APACHE_LOG_DIR}/access.log combined

 

Alias /doc/ “/usr/share/doc/”

<Directory “/usr/share/doc/”>

Options Indexes MultiViews FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

Allow from 127.0.0.0/255.0.0.0 ::1/128

</Directory>

</VirtualHost>

Important to note here is to change ServerName as per your convinince and Directory to your public of rails app.

Once this is done,

type the following to open the hosts file:

 sudo gedit /etc/hosts

 

In this file, copy the following code:

 

127.0.0.1 domain-name.com
www.domain-name.com

 

replace domain name with your desired name.

This is what you will use in browser to access the page

Activate your domain and reload the Apache Server to enable settings
Enable the site

a2ensite domain-name

 

Reload Apache2

/etc/init.d/apache2 reload

 

Finally test your app by typing www.domain-name.com and see the magic !!!

 

 

Deploying rails app with apache locally

In this post I am going to show how to make apache the server for all ruby on rails application on local machine.

In the previous post I had shown how to install apache web server ad configure it.

My software stack:

  • Ubuntu 11.04
  • ruby 1.9.2 p290.
  • Rails 3.1.10
  • rubygems 1.8.10

Lets start!

I assume that you have a rails app in the following path:

/home/username/Desktop/rails_apps/my_rails_app

It is important to note that apache serves all local request via www directory in /var.

/var/www

So the first step is to create a symbolic link:

ln -s /home/username/Desktop/rails_apps/my_rails_app/public /var/www/my_rails_app

Note that sym link should be to your appls publc folder.

Now edit the following file(you can coly paste the code)

sudo gedit /etc/apache2/sites-available/default

In this file, append the follwing line:

RailsBaseURI /my_rails_app
RailsEnv development #(if you want to run on development mode default is production)

Restart apache using

sudo /etc/init.d/apache2 restart

 Now on you browser when you type 127.0.1.1/my_rails_app
 the app should fire up !

Hurray you have done it !

————————————–warning for ruby 1.9.2p290————————————————

When I fired up my app, I got an error

Can’t convert nil to hash,

This is phsyc problem and to solve this, add the following to boot.rb

(this issue existed when I did used it )

require ‘yaml’

YAML::ENGINE.yamler = ‘syck’

Install apache web server and passenger on Ubuntu 11.04(Natty)

I just Installed apache and passenger on Ubuntu 11.04 to run and deploy my ruby on rails applications Passenger is a gem and can work with apache as well as nginx…

The reason for choosing apache is that its an industry standard.

Also one thing I did with apache was created virtual hosts and ran my apps on local machine with domains like http://www.application1.com

In this post I will list the steps I followed in order to setup apache web server and passenger.

Here is my stack:

  •  Ubuntu 11.04
  •  ruby 1.9.2 via rvm
  •  rubygems 1.8.10

I will be using rails 3.1.10(lastest this morning).!!

So lets start !!

  • Install rvm

To install rvm just type the following in the terminal(ctrl+alt+t)

 user$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

You can find more information about rvm on Here

  • Install ruby via rvm

Once rvm is installed you can install the latest or desired version of ruby by typing the following in terminal

rvm install 1.9.2

This will install ruby 1.9.2 on your system.

More information on how to make it your default ruby is available on here

  • Install rails.

just need to do

gem install rails

  • Install passenger

just do

gem install passenger

  • Install apache web server.

To install apache web server type:

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
  • Install passenger apache module.

Once this is done, we must install passenger , an apache module thats hepls us to rub rails apps on apache.

sudo passenger-install-apache2-module
  • Configure.

Finally everything is done.

When you run the passenger apache module, there would be some instructions that installer will give you.

The last thing it will tell is to paste some text in apache configuration file

The configuration file is located at /etc/apache2/apache2.conf

Now once this is done, we are ready to deploy.

This has a lot of information about deploying.

In my next post, I will show how to start rails apps on local machine with apache.

Hope it helps.!

Filter Types

A filter can take one of three forms: method reference (symbol), external class, or block.
The first is by far the most common and works by referencing a protected method some-
where in the inheritance hierarchy of the controller. In the bank example in my previous post,
both BankController and VaultController use this form.

 

  • Filter Classes

Using an external class makes for more easily reused generic filters, such as output
compression. External filter classes are implemented by having a static filter method on
any class and then passing this class to the filter method, as in code below. The name of
the class method should match the type of filter desired (e.g., before, after, around).

 

class OutputCompressionFilter
def self.after(controller)
controller.response.body = compress(controller.response.body)
end
end

class NewspaperController < ActionController::Base
after_filter OutputCompressionFilter
end

 

The method of the Filter class is passed the controller instance it is filtering. It gets
full access to the controller and can manipulate it as it sees fit. The fact that it gets an
instance of the controller to play with also makes it seem like feature envy, and frankly,
I haven’t had much use for this technique.

 

  • Inline Method

The inline method (using a block parameter to the filter method) can be used to quickly
do something small that doesn’t require a lot of explanation or just as a quick test.

class WeblogController < ActionController::Base
before_filter do
redirect_to new_user_session_path unless authenticated?
end
end

 
The block is executed in the context of the controller instance, using instance_eval.
This means that the block has access to both the request and response objects complete
with convenience methods for params, session, template, and assigns.

Filter Inheritance

Controller inheritance hierarchies share filters downward. Your average Rails application
has an ApplicationController from which all other controllers inherit, so if you
wanted to add filters that are always run no matter what, that would be the place to do so.

class ApplicationController < ActionController::Base
after_filter :compress

Subclasses can also add and/or skip already defined filters without affecting the superclass.
For example, consider the two related classes below, and how they interact.

class BankController < ActionController::Base
before_filter :audit
protected
def audit
# record this controller’s actions and parameters in an audit log
end
end

class VaultController < BankController
before_filter :verify_credentials
protected
def verify_credentials
# make sure the user is allowed into the vault
end
end

 

Any actions performed on BankController (or any of its subclasses) will
cause the audit method to be called before the requested action is exe-
cuted. On the VaultController, first the audit method is called, followed by
verify_credentials, because that’s the order in which the filters were specified. (Fil-
ters are executed in the class context where they’re declared, and the BankController
has to be loaded before VaultController, since it’s the parent class.)
If the audit method happens to call render or redirect_to for whatever reason,
verify_credentials and the requested action are never called. This is called halting
the filter chain.