Ruby: super-quick guide to push your temp sites and experiments to Heroku

Plagiarizing from here:

If you don’t have rails and ruby already on your machine:

$ gem install bundler

// rails is installed as a gem:

$ gem install rails

NOTE: If you are in a windows machine, and have trouble at this point installing rails. You need a couple of extra steps:

– Download the latest version of Ruby, and also DevKit, from:

– When prompted, extract the DevKit installation to: C:Ruby[your ruby versionDevKit

– cd to DevKit

– Run the following commands:

ruby dk.rb init

ruby dk.rb review

ruby dk.rb install

Then you can continue as if you were on a Mac or Linux setup:

$ rails new myapp

$ cd myapp

$ git init

$ git add .

$ git commit -m “initial commit”

If you want to use gitHub as your master repository:

$git remote add origin // Origin means main repository sort of

$git push -u origin master

$ gem install heroku

// This is not completely necessary, but it may be needed on your server to create the keys that will allow connection to the server:
$ heroku keys:add

$ heroku create –stack cedar

–stack cedar optional: it gives you the latest stable version of heroku

Alternatively, you can create an staging environment as follows (instead of just “heroku create”):

$heroku create your-app-name –remote staging // Remote creates an extra git repository locally where you can push changes independent from master (OPTIONAL)

$ git push staging master // Pushes to your staging env (OPTIONAL)

$ git push heroku master

NOTE: Most likely you will run into trouble here, because Heroku uses postgres instead of litesql for its database. To get past this issue:

– Edit your Gemfile as follows:

gem 'sqlite3', :group => [:development, :test]
group :production do
  gem 'thin'
  gem 'pg'
  • remove Gemfile.lock
  • run ‘bundle install –without production’
  • git add .
  • git commit -am “Using sqlite3 in local dev environment only”
  • git push heroku master

$ heroku rake db:migrate # you’ll need to do this for any schema change

Note: if you run into trouble at heroku at this point, add the following line to your Rakefile, just before “require ‘rake'”:
require ‘rake/dsl_definition’

To start using gemsets, run the following command:

$mate Gemfile (it will generate the default gem file for you)

$bundle install

And, of course, running the server:
$rails s

When you deploy to heroku, and if you forgot your temp URL, you can find out what it is by typing in the command line:

$heroku open // Open a browser with your application’s public URL

Rails: controllers

to create a new controller:

ruby script/generate controller yourcontrollernamehere

Now you just need to add a view file with the HTML under folder:


For rails 3:

$ rails generate controller Pages home contact

This adds the controller pages, and then particular pages “home” and “contact” under that controller

This will also generate the needed routes (and modify the routes file accordingly), the RSpec and views placeholders as well.

For the routers, you can just define a resource for all of them:

resources :yourControllerName

You also need to define the methods inside your new controller. For example, in the “new” method, you could do something like:

def new
  @yourcontrollername =

Another example for “create”:

def create
  @project =[:project])
  flash[:notice] = "Project has been created."
  redirect_to @project

You will also need to generate your model manually (since you are not scaffolding)

If you want to undo the controller you just created, you can run the following command:

rails destroy  controller Pages home contact

Rails 4

The format to generate new controllers is:

rails generate controller ControllerName [actions] [options]

This usually generates an empty file inside /app/controllers/user_controller.rb, you need to fill out the actions to make it useful.

The first line populates the user for edit / delete purposes:

before_action :set_user, only: [:show, :edit, :update, :destroy]

It goes along with the following method:

  def set_user
    @user = User.find(params[:id])

Here’s an example of how a typical set of action methods look inside the controller:

  def new
    @user =

  def create
    @user =
      redirect_to articles_path, notice: 'User successfully added.'
      render action: :new

  def edit

  def update
    if @user.update(user_params)
      redirect_to articles_path, notice: 'Updated user information successfully.'
      render action: 'edit'

The following code (inside your controller) tells what attributes are safe to save / update (so users can’t compromise the system by sending those manually:

def article_params

params.require(:article).permit(:title, :location, :excerpt, :body, :published_at)


Notice that using the controller generator just puts the empty files in place, it is up to you to fill out the details (as opposed to what the scaffolding does for you, which is generating all the default forms and actions and content for you)

If you are using rails-api, and just serving JSON, here’s an example for the controllers:

  def index

    all_tasks = ScheduledReportTask.where(site_id: params[:site_id])

    render :json => {:status => “ok”, :response_code => 200, :message => “ok”, :results => all_tasks }.to_json


  def show


      @scheduled_report_task = ScheduledReportTask.find(params[:id])

      if @scheduled_report_task.site_id == @global_site_id

        render :json => self.prepare_return(@scheduled_report_task)





      render :json => self.package_error