Created
February 23, 2020 10:34
-
-
Save jonny-gates/085c52cf343fcbf949f6839068b90406 to your computer and use it in GitHub Desktop.
Rails Quiz Correction
This file contains 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
# Q1 - How do you create a Rails app? | |
# TODO: rails? | |
rails new project-name --database=postgresql | |
# Q2 - How do you start coding a Rails project? Give the right sequence. | |
# '1. Coding the views?' | |
# '2. Coding the controllers?' | |
# '3. Coding the models?' | |
# TODO: 1, 2, 3? | |
3,2,1 | |
# Q3 - How do you generate a Song model with a title and a year? | |
# TODO: rails? | |
rails g model song title:string year:integer | |
# What are the 2 created files? | |
# TODO: | |
1. app/models/song.rb | |
2. db/migrate/8765678909876_create_songs.rb | |
# What is the rails command you should type then? | |
# TODO: rails? | |
rails db:migrate | |
# Q4 - How do you add a category (ex: “rock” , “electro” , etc..) to your songs table using the right Rails generator? | |
# TODO: rails? | |
rails g migration AddCategoryToSongs category:string | |
# add_column :songs, :category, :string | |
# What is the created file? | |
# TODO: | |
timestamp_add_category_to_songs.rb | |
# What is the rails command you should type again? | |
# TODO: rails? | |
rails db:migrate | |
# Q5 - Add a presence validation on song titles & crash-test your model in the console | |
class Song < ApplicationRecord | |
# TODO | |
validates :title, presence: true | |
end | |
# Now crash-test your model: | |
# TODO: | |
new_song = Song.new(title: 'some title', category: 'blues') | |
new_song.valid? => true | |
test_song = Song.new(category: 'some title') | |
test_song.valid? | |
test_song.save | |
test_song.errors | |
# Q6 - What is the Rails flow you need to follow again and again? Give the correct order | |
# 1. The Router is routing the HTTP request to "controller#action". | |
# 2. The action is getting data from models. | |
# 3. Everything starts with an HTTP Request. | |
# 4. The action is rendering the view. | |
# TODO: 1, 2, 3, 4? | |
3,1,2,4 | |
# Q7 - What are the 4 different parts inside an HTTP request? | |
verb/method # GET/POST/PUT/PATCH/DELETE | |
path # URL | |
headers | |
body | |
# Q8 - Are the HTTP requests in the following 2 routes the same? Why? | |
# get "/songs" => "songs#index" | |
# post "/songs" => "songs#create" | |
# TODO: | |
No - they share the same path, but have different verbs. | |
# Q9 - What’s the difference between a GET and a POST request? | |
# TODO: | |
get reads data and post sends data | |
# Q10 - Complete the controller code using the correct params key? | |
# GET /search?query=thriller | |
class SongsController < ApplicationController | |
def search | |
# TODO | |
@songs = Song.where(title: params[:query]) | |
end | |
end | |
# Q11 - Complete the controller code using the correct params key? | |
# GET /songs/named/thriller | |
class SongsController < ApplicationController | |
def search | |
# TODO | |
@songs = Song.where(title: params[:name]) | |
end | |
end | |
# Q12 - What are the 7 CRUD routes generated by the resources method in Rails? | |
get 'songs', to: 'songs#index' | |
get 'songs/:id', to: 'songs#show' | |
get 'songs/new', to: 'songs#new' | |
post 'songs', to: 'songs#create' | |
get 'songs/:id/edit', to: 'songs#edit' | |
patch 'songs/:id', to: 'songs#update' | |
delete 'songs/:id', to: 'songs#destroy' | |
# Q13 - How do you print your routes and their URL prefix helpers? | |
# TODO: | |
rails routes | |
# Q14 - How do you generate a controller for your songs? | |
# TODO: rails? | |
rails generate controller songs | |
# Q15 - Implement the Read actions in your songs controller? | |
class SongsController < ApplicationController | |
# TODO: Implement the read actions | |
def index | |
@songs = Song.all | |
end | |
def show | |
@song = Song.find(params[:id]) | |
end | |
end | |
# Q16 - What are the 2 requests needed to create a new song? Implement the songs#new and songs#create actions. | |
class SongsController < ApplicationController | |
def new | |
# TODO | |
@song = Song.new | |
end | |
def create | |
# TODO | |
@song = Song.new(song_params) | |
if @song.save | |
redirect_to song_path(@song) | |
else | |
render :new | |
end | |
end | |
private | |
def song_params | |
# TODO | |
params.require(:song).permit(:title, :year, :category) | |
end | |
end | |
# Q17 - Why do we have to filter parameters using “strong params” in the controller? | |
# TODO: | |
For security purposes, Rails will only permit identified params | |
# Q18 - Hard question: What is the HTML generated? | |
# @song = Song.new | |
# TODO: | |
Ruby object | |
# Now what is the HTML generated by? | |
# <%= form_for @song do |f| %> | |
# <%= f.text_field :title %> | |
# <%= f.submit %> | |
# <% end %> | |
# Fill in the blanks | |
<form action="songs/new" method="post"> | |
<input type="text" name="song[title]" value=""> | |
<input type="submit" value="Create song"> | |
</form> | |
# Q19 - Hard question: What is the HTML generated? | |
# @song # => <#Song: id: 18, title: "Hey jude", year: 1968, category: "rock"> | |
# <%= form_for @song do |f| %> | |
# <%= f.text_field :title %> | |
# <%= f.submit %> | |
# <% end %> | |
<form action="/songs/18" method="patch"> | |
<input type="text" name="song[title]" value="Hey Jude"> | |
<input type="submit" value="Update song"> | |
</form> | |
# Q20 - Now you want to add reviews to your app. Here are some constraints | |
# Generate your Review model in the terminal. It should have only a content:string and a song:references (= the foreign key). | |
# TODO: rails? | |
rails g model review content:string song:references | |
# Run the migration | |
# TODO: rails? | |
rails db:migrate | |
# Add validation/associations | |
# - Add a validation for the presence of a content | |
# - Add associations between Review and Song | |
class Song < ApplicationRecord | |
# TODO | |
has_many :reviews | |
end | |
class Review < ApplicationRecord | |
# TODO | |
belongs_to :song | |
validates :content, presence: true | |
end | |
# TODO: Generate the reviews controller | |
rails g controller reviews | |
# Add the necessary routes | |
resources :songs do | |
# TODO | |
resources :reviews, only: [:new, :create] | |
end | |
# Now code your controller | |
class ReviewsController < ApplicationController | |
before_action :set_song | |
def new | |
# TODO | |
@review = Review.new | |
end | |
def create | |
# TODO | |
@review = Review.new(review_params) | |
@review.song = @song | |
if @review.save | |
redirect_to song_path(@song) | |
else | |
render :new | |
end | |
end | |
private | |
def set_song | |
@song = Song.find(params[:song_id]) | |
end | |
def review_params | |
params.require(:review).permit(:content) | |
end | |
end | |
# Add a song's review | |
<h1><%= @song.title %></h1> | |
<p><%= @song.year %></p> | |
<p><%= @song.category %></p> | |
<h2>Here are the reviews for this song:</h2> | |
<% @song.reviews.each do |review| %> | |
<p><%= review.content %></p> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment