-
Notifications
You must be signed in to change notification settings - Fork 0
Rails
Tim edited this page Sep 11, 2015
·
44 revisions
How to build a Rails App, in a few easy steps:
-
$ gem install rails- once only puts rails on your machine. $ rails new <app-name> -d postgresql -T
- This will create your app directory.
-
$ bin/rake db:create- create development database -
$ bin/rake db:create RAILS_ENV=test- create test database - Testing - add to gem file
group :test do
gem 'rspec-rails'
gem 'capybara'
end-
$ bundle- install gems -
$ bin/rails g rspec:install- create rspec files and folders - Add to
.rspecfile
- 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.
- 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- 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
-
$ bin/rails g controller <entity>s- Create controller -
$ touch app/views/<entity>s/index.html.erb- Create view -
$ bin/rails g model <entity> name:string- Create model -
$ bin/rake db:migrate- create table from model - Create
index.html.erbfile
- 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 %>- Update controller
def index
@<entity>s = <Entity>.all
end- 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 %>- 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>- 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- To come later!
<% 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
-
gem 'shoulda'add new Gem to gemfile. - 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- 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- Update
routes.rb???
- nested routes - maybe nested shallow routes
- A blog has many comments:
$ bin/rails g migration AddBlogIdToComments Blog:belongs_to- add association - Update models
has_many :comments
belongs_to :blog####Other Stuff
####Active Record
####More Rails
- Rails Views
- Rails Models
- Rails Controllers
- Rails Routes
- Rails Error Handling
- Rails Migrations & Scaffolding
####Other common commands to be used:
-
$ bin/rake routes- view all routes. -
$ bin/rails serverstarts rails server -
$ rails d- destroy what you have done before. -
$ rails c- Rails console, gives you scratch pad in dev