How to Rail Rails Before Rails Rails You.

D I S C L A I M E R :
I am not an expert in Rails. I can only provide information that I believe I understand so far. This was created mainly to test my own understanding of the material. If you find anything in this article that contains false information or seems misleading, please correct me! Thank you.
rails new [NameOfYourApplication]rails new Doggorails new AdoptDoggo
rails g resource [NameOfTable] [columnname]:[datatype] [columnname]:[datatype]rails g resource Dog name:string breed:string age:integer
integer      (whole numbers)
primary_key (id number)
float (0.0)
boolean (true || false)
string ("text")
text ("a long description")
date (YYYY-MM-DD)
time (HH:MM:SS[.NNNNN])
datetime (YYYY-MM-DD HH:MM:SS)
timestamp (YYYY-MM-DD HH:MM:SS UTC)

SEED YOUR DATA:

Dog.destroy_all
Dog.create(name: "Nugget", breed: "Siberian Husky", age: 1)
rails g migration add_[ColumnName]_to_[TableName] [columnname]:[datatype]ORrails g migration AddColumnNameToTableName columnname:datatype
rails g migration add_url_to_dogs url:stringrails g migration AddUrlToDogs url:string
Dog.create(name: "Nugget", breed: "Siberian Husky", age: 1, 
url: "https://www.flickr.com/photos/142916389@N03/50352181986/")

METHODS FOR YOUR CONTROLLER:

def index
@dogs = Dog.all
end
def show
id = params[:id]
@dog = Dog.find(id)
end
def new
@dog = Dog.new
end
def create
name = params["dog"]["name"]
breed = params["dog"]["breed"]
age = params["dog"]["age"]
url = params["dog"]["url"]
dog = Dog.create(name: name, breed: breed, age: age, url: url)
redirect_to dog_path(dog)
end
def edit
@dog = Dog.find(params[:id])
render :edit
end
def update
@dog = Dog.update(params[:id])
redirect_to dog_path(@dog)
end
def destroy
@dog = Dog.destroy(params[:id])
redirect_to dogs_path
end

WHAT’S IN YOUR HTML:

<h1>List of Dogs</h1>
<%@dogs.each do |dog| %>
<%=link_to dog.name, dog_path(dog)%>
<%end%>
<li>Name: <%= @dogs.name %></li>
<li>Breed: <%= @dogs.breed %></li>
<li>Age: <%= @dogs.age %></li>
%= image_tag("#{@dogs.url}", height: "30%", width: "30%")%>
<%= link_to("Back To Index", dogs_path)%><%= link_to "Delete Dog", dog_path(@dog.id), method: "delete",
data: {:confirm => "Are you sure?"}%>
<%= link_to "Edit Dog", edit_dog_path %>
<form action="/dogs" method="POST"><label for="name">Doggos Name:</label>
<input type="text" id="name" name="name">
<label for="breed">Breed:</label>
<input type="text" id="breed" name="breed">
<label for="age">Age:</label>
<input type="text" id="age" name="age">
<label for="url">URL:</label>
<input type="text" id="url" name="url">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %><input type="submit" value="DOGGO!"></form>
<h1>Add a new Dog!</h1>
<%= form_for(@dogs) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :breed %>
<%= f.text_field :breed %>
<%= f.label :age %>
<%= f.number_field :age %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit 'DOGGO!' %>
<% end %>
<%= f.collection_select(:breed_id, Breed.all, :id, :name) %>
<%= f.collection_check_boxes(:breed_id, Breed.all, :id, :name) %> 
<h1>Edit Doggo!</h1>
<%= form_for(@dogs) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :breed %>
<%= f.text_field :breed %>
<%= f.label :age %>
<%= f.number_field :age %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit 'DOGGO!' %>
<% end %>

UNDERSTANDING THE ROUTES:

How it feels when your routes stop you from moving forward.
get '/dogs', to: 'dogs#index'
get 'dogs/:id', to: 'dogs#show', as: 'dog'
get '/dogs/new', to: 'dogs#new', as: 'new_dog'
post '/dogs/', to: 'dogs#create'

RESOURCES:

get '/dogs', to: 'dogs#index'
get 'dogs/:id', to: 'dogs#show', as: 'dog'
get '/dogs/new', to: 'dogs#new', as: 'new_dog'
post '/dogs/', to: 'dogs#create'
resources: :dogs, only: [:index, :show, :new, :create]
resources: :dogs, except: [:edit, :update, :destroy]

BEFORE ACTION:

class DogsController < ApplicationController
before_action :find_dog, only: [:show, :edit, :update, :destroy]
def index
@dogs = Dog.all
end

def edit
end
.....end
 privatedef find_dog
@dog = Dog.find(params[:id])
end

STRONG PARAMS:

 privatedef dog_params
params.require(:dog).permit(:name, :breed, :age, :url)
end
def create
@dog = Dog.new(dog_params)
if @dog.save
redirect_to dog_path(@dog.id)
else
render :new
end
end
def update
@dog = Dog.update(dog_params)
if @dog.save
redirect_to dog_path(@dog)
else
render :new
end
end

VALIDATIONS:

validates :name, presence: true, uniqueness: {case_sensitive: false}validates :breed, presence: truevalidates :bio, length: {minimum: 30}validates :age, numericality: {less_than: 25, message: "%{age} is way too high for a doggo"}validates :good_with_kids, presence: true, inclusion: {in: [true, false], message: "%{value} is not true or false"}
<% if @dog.errors %>
<% @dog.errors.full_messages.each do |error|%>
<%= error %>
<% end %>
<% end %>
There’s more…?!
validate :dogs_dont_start_with_qdef dogs_dont_start_with_q
if name.downcase.starts_with?('q')
errors.add(:name, "can't start with q")
end
end

RENDER PARTIALS:

<%= form_for(@dogs) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :breed %>
<%= f.text_field :breed %>
<%= f.label :age %>
<%= f.number_field :age %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit 'DOGGO!' %>
<% end %>
<%= render partial: "dog_form", locals: {button_text: "Edit this dog!"} %>

CREATING AN ABOUT PAGE:

rails g controller [NameOfController]rails g controller Application
def about
@num_dogs = Dog.count
render :about
end
<h1>About my Dogs</h1>
<div>Total number of dogs: <%= @num_dogs %></div>
You got this!

Software Engineer in the making.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store