Skip to content

Instantly share code, notes, and snippets.

@raghubetina
Last active December 25, 2017 20:53
Show Gist options
  • Save raghubetina/a276f1e0a78720416c59 to your computer and use it in GitHub Desktop.
Save raghubetina/a276f1e0a78720416c59 to your computer and use it in GitHub Desktop.
Rails Basic Commands

Rails Basic Commands

Generate or Clone an application

Generate a brand new application

cd to the folder where you want to store your new application. For me, this is ~/code. Then,

rails new my_app

This creates a subfolder called my_app and puts all of the boilerplate for a Rails application inside.

For all subsequent commands, you must cd in to the application folder first.

Or if you cloned an existing application

cd in to the folder you just downloaded. Then,

bundle install

The bundle install (or just bundle for short) command fetches all of the 3rd party Ruby code that the application depends upon and installs it on your machine. You only need to do this once per application.

Start the web server

rails server

or rails s for short.

If the server started successfully, you should see output like the following. It may take a moment.

=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-04-27 10:14:28] INFO  WEBrick 1.3.1
[2015-04-27 10:14:28] INFO  ruby 2.2.1 (2015-02-26) [x86_64-darwin14]
[2015-04-27 10:14:28] INFO  WEBrick::HTTPServer#start: pid=15767 port=3000

and then the cursor should be blinking at the bottom, but not at the end of your usual command prompt. This window is now dedicated to the web server app, and you can't run any other commands in it until you shut down the server.

If you need to run other command line stuff, open up a new window or tab.

If the server failed to start

  1. If you got a "address already in use" error, then you must have an old rails server running in another window or tab somewhere. Find it and Ctrl-C (or just close the tab).
  2. If you have a syntax error in config/routes.rb, then the server will refuse to even start up. See if the error message complains about a line in that file.

Shut down the web server

Ctrl-C. On Windows, you may be asked whether to terminate the job; say yes.

Start the Rails Console

rails console

or rails c for short.

Rails Console is a superpowered version of IRB, handy for testing snippets of code.

Set up the database

If the application you cloned requires database setup, then

rake db:migrate

will create the tables and columns. If I provided some starter dummy data for you, then

rake db:seed

will quickly pre-populate the tables for you, so you don't have to spend an hour in rails console adding rows.

FAQ

I am getting an error relating to SSL when I bundle

If your bundle install fails with a message like this:

Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify

then open the file called Gemfile in the root folder of the application with Atom and change

source 'https://rubygems.org'

to

source 'http://rubygems.org'

(remove the "s"). Save and then bundle install again.

This is only a temporary fix. Long term, you should find a permanent solution or switch to a different development environment (Linux, OS X, or a cloud environment like Nitrous.io or Cloud9).

I am getting an error relating to ExecJS or TypeError: Object doesn't support this property or method

Go in to the app/assets/javascripts/application.js file and delete the line

//= require_tree .

Refresh the page in Chrome; it should now be okay.

If you are still experiencing an error, try the following also:

  1. In Gemfile, delete the line

    gem 'turbolinks'
    
  2. In app/assets/javascripts/application.js, delete the line

    //= require turbolinks
    
  3. bundle install and restart your server.

@jkatz1985
Copy link

Are you in the new file you created? I got that 'could not locate' error when I was in the parent folder

@ebellendir
Copy link

I needed to get rid of "turbolinks" to get devise to work correctly. I was getting the ExecJS error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment