Skip to content
Tim edited this page Sep 11, 2015 · 44 revisions

How to build a Rails App, in a few easy steps:

Setup

  1. $ gem install rails - once only puts rails on your machine.
  2. $ rails new <app-name> -d postgresql -T
  • This will create your app directory.
  1. $ bin/rake db:create - create development database
  2. $ bin/rake db:create RAILS_ENV=test - create test database
  3. Testing - add to gem file
group :test do
  gem 'rspec-rails'
  gem 'capybara'
end
  1. $ bundle - install gems
  2. $ bin/rails g rspec:install - create rspec files and folders
  3. Add to .rspec file
  1. Then, in your spec/rails_helper.rb file add the following require statement below the other require statements: require 'capybara/rails' - This lets you use Capybara in your testing environment for the purpose of writing end user acceptance tests.

CRUD - List(Index page)

  1. Build feature tests here: spec/features/<entity>s_feature_spec.rb:
require 'rails_helper'

feature '<entity>s' do
  context 'no <entity>s have been added' do
    scenario 'should display a prompt to add a <entity>' do
      visit '/<entity>s'
      expect(page).to have_content 'No <entity>s yet'
      expect(page).to have_link 'Add a <entity>'
    end
  end
end
  1. Changes in config/routes
  • get 'locations' => 'locations#index' - which links up the '/restaurants' URL to the index action on the restaurants controller.
  • resources :locations - shortcut for creating all the commonly used routes associated with a resource
  1. $ bin/rails g controller <entity>s - Create controller
  2. $ touch app/views/<entity>s/index.html.erb - Create view
  3. $ bin/rails g model <entity> name:string - Create model
  4. $ bin/rake db:migrate - create table from model
  5. Create index.html.erb file
  • contains link to show records
<% if @<entity>s.any? %>
  <% @<entity>s.each do |<entity>| %>
    <%= link_to <entity>.name, <entity>_path(<entity>) %>
  <% end %>
<% else %>
  <h1>No <entity>s yet</h1>
<% end %>
<br>
<%= link_to "Add a <entity>", new_<entity>_path %>
  1. Update controller
  def index
    @<entity>s = <Entity>.all
  end

CRUD - Create(new) & Read(show)

  1. new form (using simple forms gem) app/views/<entity>/new.html.erb
<%= simple_form_for @location do |f| %>
  <%= f.input :name, label: 'The name of your location' %>
  <%= f.input :description, placeholder: 'fill description of location here' %>
  <%= f.input :film, label: 'What film is it from' %>
  <%= f.input :latitude %>
  <%= f.input :longitude %>
  <%= f.input :address, hint: 'this will be auto populated from the map'%>
  <%= f.button :submit %>
<% end %>
  1. show form app/view/<entity>/show.html.erb
<p><%= @location.name %></p>
<p><%= @location.description %></p>
<p><%= @location.film %></p>
<p><%= @location.latitude %></p>
<p><%= @location.longitude %></p>
<p><%= @location.address %></p>
  1. Update controller app/controllers/<entity>sController.rb
  def new
    @location = Location.new
  end

  def create
    Location.create(location_params)
    redirect_to '/locations'
  end

  # define which params we are going to allow us to pass to controller, without this security flaw.
  def location_params
    params.require(:location).permit(:name, :description, :film, :latitude, :longitude, :address)
  end

  def show
    @location = Location.find(params[:id])
  end

Update & Delete

  1. To come later!

Random bit of error handling code?

<% if @location.errors.any? %>
  <section id="errors">
    <h2> <%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved: </h2>
    <ul>
      <% @location.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </section>
<% end %>

###Associations

  1. gem 'shoulda' add new Gem to gemfile.
  2. Feature Test
require 'rails_helper'

feature 'reviewing' do
  before {Restaurant.create name: 'KFC'}

  scenario 'allows users to leave a review using a form' do
     visit '/restaurants'
     click_link 'Review KFC'
     fill_in "Thoughts", with: "so so"
     select '3', from: 'Rating'
     click_button 'Leave Review'

     expect(current_path).to eq '/restaurants'
     expect(page).to have_content('so so')
  end

end
  1. Unit Test (utilises shoulda) in spec/models/<entity>_spec.rb
  • the entity is described in the singular.
require 'rails_helper'

describe Blog, type: :model do
  it { is_expected.to have_many :comments }
end
  1. Update routes.rb ???
  • nested routes - maybe nested shallow routes
  1. A blog has many comments: $ bin/rails g migration AddBlogIdToComments Blog:belongs_to - add association
  2. Update models
  has_many :comments
  belongs_to :blog

####Other Stuff

  1. Add Devise
  2. Add Paperclip
  3. Javascript & Rails Comms

####Active Record

  1. Data types
  2. Joining tables
  3. Active Record Commands

####More Rails

  1. Rails Views
  2. Rails Models
  3. Rails Controllers
  4. Rails Routes
  5. Rails Error Handling
  6. Rails Migrations & Scaffolding

####Other common commands to be used:

  1. $ bin/rake routes - view all routes.
  2. $ bin/rails server starts rails server
  3. $ rails d - destroy what you have done before.
  4. $ rails c - Rails console, gives you scratch pad in dev

Clone this wiki locally