Skip to content

Instantly share code, notes, and snippets.

@SanjeQi
Last active December 4, 2018 11:39
Show Gist options
  • Save SanjeQi/2bfe4c5a411ba6bf48542161e4fdd97c to your computer and use it in GitHub Desktop.
Save SanjeQi/2bfe4c5a411ba6bf48542161e4fdd97c to your computer and use it in GitHub Desktop.
f f f f f f important
rails destroy resource Dog #get rid of everything ralated to Dog
--------------------------------------------------------------------------
rails new dog-app -T #-T to create without the test framework . Test with byebug
cd dog-app/
rails g model dog name motto # Generate the model for the application (model should be singular)
# This creates 2 files:
# app/models/dog.rb
# db/migrate/[date_time]_create_dogs.rb
# The dog table in our database will have 2 fields: name and motto.
rails g controller dogs index show new edit #Generate the controller for the application (controller should be plural):
# Other than the “dogs_controller.rb” that gets created,
#a “dogs” folder along with four files we requested gets added within the views folder:
# index.html.erb
# show.html.erb
# new.html.erb
# edit.html.erb
rails g migration add_dog_id_column_to_dogs dog_id:integer # add collumn dog_id to dogs table
rails g resource Dog name:sting motto:sting --no-test-framework # generate rails g model and controller and route
# config/routes.rb file also gets automatically updated:
Rails.application.routes.draw do
get 'dogs/index'
get 'dogs/show'
get 'dogs/new'
get 'dogs/edit'
end
# all CRUD operations
Rails.application.routes.draw do
resources :dogs
end
#migrate our data. Double check our migration file to make sure it looks appropriate:
class CreateDogs < ActiveRecord::Migration[5.2]
def change
create_table :dogs do |t|
t.string :name
t.string :motto
t.timestamps
end
end
end
### migrate
rake db:migrate #create the table in our SQLite database based on what is in our db/migrate/[date_time]_create_dogs.rb file.
##create seed data db/migrate/seeds.rb
Dog.create(name: 'Nala', motto: 'Born to be wild')
Dog.create(name: 'Alex', motto: 'Calm as can be')
##seed . to add the dogs to the database.
rake db:seed
rails console or rails c #check if the dogs have been added to your database, run:
# Type in Dog.all and you should see that the 4 dogs have been added.
#check routes
rails routes
rake routes
#CRUD functions. Create Read Update Delete
#Goal: List all dogs -Read
#Controller — app/controllers/dogs_controller.rb, index method
def index
@dogs = Dog.all
end
#Index — views/dogs/index.html.erb
<ul>
<% @dogs.each do |dog| %>
<li><%= link_to dog.name, dog_path(dog) %></li>
<% end %>
</ul>
#Goal: Show details of specific dog -Read
#Controller — app/controllers/dogs_controller.rb, show method
def show
@dog = Dog.find(params[:id])
end
#Show — views/dogs/show.html.erb
<h1><%= @dog.name %></h1>
<h4>"<%= @dog.motto %>"</h4>
#Goal: Create a new dog - CREATE
#Controller — app/controllers/dogs_controller.rb, new and create methods
def new
@dog = Dog.new
end
def create
dog = Dog.create(dog_params)
redirect_to dogs_path
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
#New — views/dogs/new.html.erb
<h3>Create a Dog</h3>
<%= form_with model: @dog do |form| %>
<%= form.text_field :name, placeholder: "name" %>
<%= form.text_field :motto, placeholder: "motto" %>
<%= form.submit %>
<% end %>
#Goal: Update details for specific dog - UPDATE
#Controller — app/controllers/dogs_controller.rb, edit and update methods
def edit
@dog = Dog.find(params[:id])
end
def update
@dog = Dog.find(params[:id])
@dog.update(dog_params)
redirect_to dog_path(@dog)
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
#Edit — views/dogs/edit.html.erb
<h3>Update Dog Details</h3>
<%= form_with model: @dog do |form| %>
<%= form.text_field :name, placeholder: "name" %>
<%= form.text_field :motto, placeholder: "motto" %>
<%= form.submit %>
<% end %>
#Goal: Remove a dog from the database - DELETE
#Controller — app/controllers/dogs_controller.rb, destroy method
def destroy
@dog = Dog.find(params[:id])
@dog.destroy
redirect_to dogs_path
end
#Show — views/dogs/show.html.erb
<%= link_to 'Remove', @dog, method: :delete, data: { confirm: 'Are you sure?' } %>
##############################################################
#Here is the current Controller code:
class DogsController < ApplicationController
def index
@dogs = Dog.all
end
def show
@dog = Dog.find(params[:id])
end
def new
@dog = Dog.new
end
def create
dog = Dog.create(dog_params)
redirect_to dog_path(dog)
end
def edit
@dog = Dog.find(params[:id])
end
def update
@dog = Dog.find(params[:id])
@dog.update(dog_params)
redirect_to dog_path(@dog)
end
def destroy
@dog = Dog.find(params[:id])
@dog.destroy
redirect_to dogs_path
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
end
#You’ll notice that this doesn’t completely follow DRY because we have the
#same line of code to find the dog written 4 times.
# Let’s refactor the Controller file:
class DogsController < ApplicationController
before_action :current_dog, only: [:show, :edit, :update, :destroy]
def index
@dogs = Dog.all
end
def show
end
def new
@dog = Dog.new
end
def create
dog = Dog.create(dog_params)
redirect_to dog_path(dog)
end
def edit
end
def update
@dog.update(dog_params)
redirect_to dog_path(@dog)
end
def destroy
@dog.destroy
redirect_to dogs_path
end
private
def dog_params
params.require(:dog).permit(:name, :motto)
end
def current_dog
@dog = Dog.find(params[:id])
end
end
#Let’s add some more links to our index.erb file to connect all the other pages in one area.
<table>
<% @dogs.each do |dog| %>
<tr>
<td><%= link_to dog.name, dog_path(dog) %></td>
<td><%= button_to 'Edit', edit_dog_path(dog), method: 'get'%></td>
<td><%= button_to 'Remove', dog_path(dog), method: 'delete', data: { confirm: 'Are you sure?' } %></td>
<% end %>
</table>
<%= link_to "Add a New Dog", new_dog_path %>
# for testing use: byebug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment