Ruby on Rails
Earth, by Tim Rourke
Note: If you do not have Rails installed, you must install the gem: gem install rails!
This is a step-by-step guide to creating a new Rails application. Let's get started! To create a new rails app, you use the following syntax:
rails new app_name -d postgresql -TWhere app_name is the name of your application. For example, to create an app called Coffee_Shop, I would enter the following into my terminal: rails new Coffee_Shop
The -d postgresql tells rails to configure the application to use postgres
The -T tells rails to ommit including the default rails testing framework
This will create a new Ruby on Rails application in a new desdecent folder named after my app's name in the rails new command. Enter ls to see the folder and then cd your_app_name to move into your new app. To open your new Rails project, in atom, enter the following command: atom .
Locate your application's Gemfile. Inside of it, you will comment out the following gem(s):
gem 'coffee-rails', '~> 4.1.0'You will also add the following gem(s):
gem 'rspec-rails'Once you've edited your Gemfile, it is time to feel the ground shake and bundle!
rack).rails serverrack, your project will be served at: http://localhost:3000/Note the difference in ports: 9292 vs 3000
To view your project in the console you will run rails console
To generate a model for your project, you can run the following command:
rails generate model Name product_name:string description:textNote: because we strive for efficiency, we'r going to start calling our rake commands in the bin/ directory inside of our project!!!
To automatically generate SQL database tables based on your models, you can run the following command:
bin/rake db:createTo run any changes in your migration scripts, you must run:
bin/rake db:migrateFinally, to create test entries for RSpec tests, you must run the following command:
bin/rake db:test:prepareCreate a controller to go along with our model.
bin/rails generate controller roasts index show edit newWhere rails generate controller specifies that we're creating a new controller. roasts is the name. index show edit new are the actions to be generated in our controller.
We can also delete actions:
bin/rails d controller roasts index show edit new
bin/rake routes returns a list of routes in your Rails project.
Once you have included rspec in your project, you may generate unit test specs using the following command(s) in your termnial:
rails generate rspec:install
To run your tests in the command line:
bin/rake specrequire 'rails_helper'
RSpec.describe Object, type: :model do
describe 'given an object' do
before do
@object = Object.new()
end
describe '#to_s' do
it 'returns an object is an "Object"' do
expectation = @object.to_s # should return 'Object'
actual = 'Object'
expect(expectation).to eq(actual)
end
end
end
end
. is a class method and # is an instance method