-
-
Save kbberker/6e60cb600ee91d4792304858087f41e6 to your computer and use it in GitHub Desktop.
f f f f f f important
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails destroy resource Dog #get rid of everything related to Dog | |
-------------------------------------------------------------------------- | |
rails new dog-app -T #-T to create without the test framework . Test with byebug | |
cd dog-app/ | |
# To create a rails api app with postgresql as the database: | |
rails new <my_app_name> --database=postgresql --api | |
# Navigate to your Gemfile and uncomment gem 'rack-cors' | |
# Make sure you add the gem 'active_model_serializers' to your Gemfile. | |
# game:references means that the new model (player) "belongs to" game | |
rails g model player name is_dasher:boolean game:references | |
# When creating a controller adding api/v1/... creates namespacing | |
rails g controller api/v1/players | |
# routes.rb file will have this to start with | |
Rails.application.routes.draw do | |
namespace 'api' do | |
namespace 'v1' do | |
resources :players | |
end | |
end | |
end | |
# Important to run rails db:create when using postgresql | |
rails db:create && rails db:migrate | |
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 name:string motto:string # 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?' } %> | |
#How to delete using good old forms | |
<%= form_tag song_path(@song), method: "delete" do %> | |
<%= submit_tag "Delete #{@song.title}" %> | |
<% end %> | |
############################################################## | |
#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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= f.collection_select :dog_id, @dogs, :id, :name %> #Dropdown selection | |
<%# f.collection_check_boxes :dog_id, @dogs, :id, :name %> #Checkboxes | |
# Basic forms with rails | |
<%= form_for @song do |f| %> | |
<%= f.label :title %> | |
<%= f.text_field :title %> | |
<br /> | |
<%= f.label :artist %> | |
<%= f.text_field :artist %> | |
<br /> | |
<%= f.collection_select :genre_id, @genres, :id, :name %> | |
<br> | |
<%# When creating forms, remember that the ruby helpers just create HTML, so can %> | |
<%# use regular HTML tags as well. %> | |
<label for="song_notes">Song Notes: </label> <br> | |
<input type="text" name="song[note_contents][]" id="song_notes"> <br> | |
<input type="text" name="song[note_contents][]"> <br> | |
<input type="text" name="song[note_contents][]"> <br> | |
<%= f.submit %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment