Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created March 1, 2019 21:53
Show Gist options
  • Save caioertai/3a4147b5179afaee95b9aff24024ddeb to your computer and use it in GitHub Desktop.
Save caioertai/3a4147b5179afaee95b9aff24024ddeb to your computer and use it in GitHub Desktop.
Answers for Rails Quiz

1.

rails new app_name --options-flags

2.

3 2 1

3.

rails g model Song title:string year:integer

app/models/song.rb
db/migrate/95347573749_create_songs.rb

rails db:migrate

4.

rails g migration AddCategoryToSongs category:string

db/migrate/20190301204229_add_category_to_songs.rb

rails db:migrate

5.

class Song < ApplicationRecord
  validates :title, presence: true
end
  song = Song.new(name: '')
  song.valid?
  # => false
  song.errors
  # => an error object with more info

6.

3 1 2 4

7.

VERB (GET, POST, PATCH, DELETE, ....)
URL (procotol/host or domain/path/query)
HEADERS (stuff... user agent, authentication, etc)
BODY (content)

8.

No, because the verb is different.

9.

GET intends to request information, POST intends to send information.

10.

# params[:query] => 'thriller'
@songs = Song.where(title: params[:query])

11.

# params[:name] => 'thriller'
@songs = Song.where(title: params[:name])

12.

# Create
get    'songs/new',      to: 'songs#new'
post   'songs',          to: 'songs#create'

# Read
get    'songs',          to: 'songs#index'
get    'songs/:id'       to: 'songs#show', as: 'song'

# Update
get    'songs/:id/edit', to: 'songs#edit'
patch  'songs/:id',      to: 'songs#update'

# Destroy
delete 'songs/:id',      to: 'songs#destroy'

13.

rails routes

14.

rails g controller Songs

15.

def show
  @song = Song.find(params[:id])
end

def index
  @songs = Song.all
end

16.

def new
  @song = Song.new
end

def create
  @song = Song.new(song_params)
  if @song.save
    redirect_to @song
  else
    render :new
  end
end

def song_params
  params.require(:song).permit(:title, :year, :category)
end

17.

It's unsafe to fully trust the parameters that user sends.

18.

action -> 'songs'
name   -> 'song[title]'
value  -> ''

19.

action -> 'songs/18'
name   -> 'song[title]'
value  -> 'Hey Jude'

20.

rails g model review content song:references

rails db:migrate

class Song < ApplicationRecord
  has_many :reviews
end

class Review < ApplicationRecord
  belongs_to :song
  validates :content, presence: true
end

rails g controller reviews new create

resources :songs do
  resources :reviews, only: [:new, :create]
  # Next line creates an array of symbols
  # %i[new create]
end
def new
  @review = Review.new
end

def create
  @review = Review.new(review_params)
  @review.song = @song
  if @review.save
    redirect_to @song
  else
    render :new
    # render 'songs/show' # <- in case the form for a new review was in the song show page
  end
end
<% @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