Instantly share code, notes, and snippets.

View quotes_controller.rb
class QuotesController < ApplicationController
before_action :set_quote, only: [:show]
before_action :require_login
skip_before_action :require_login, only: [:index, :show]
def show
end
def new
View artists_controller.rb
class ArtistsController < ApplicationController
before_action :set_artist, only: [:show, :edit, :update, :destroy]
skip_before_action :require_login, only: [:index, :show]
def index
@artists = Artist.all
end
def show
end
View artist.rb
class Artist < ApplicationRecord
belongs_to :user
has_many :spoken_quotes, class_name: "Quote", foreign_key: :speaker_id
has_many :topic_quotes, class_name: "Quote", foreign_key: :topic_id
validates :user_id, presence: true
validates :name, presence: true, length: { maximum: 120 },
uniqueness: { case_sensitive: false }
end
View quote.rb
class Quote < ApplicationRecord
belongs_to :user
belongs_to :speaker, class_name: "Artist"
belongs_to :topic, class_name: "Artist"
belongs_to :genre
delegate :medium, to: :genre, allow_nil: true
validates :user_id, presence: true
View _form.html.erb
<%= form_for(@quote) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class='row'>
<div class='col'>
<div class='form-group'>
<%= f.label :speaker, 'Who said it?' %>
<%= f.collection_select :speaker_id, Artist.order(:name), :id, :name,
{prompt: 'Select an artist'}, {class: 'form-control select-artist'} %>
</div>
View artists_controller.rb
class ArtistsController < ApplicationController
before_action :set_artist, only: [:show, :edit, :update, :destroy]
skip_before_action :require_login, only: [:index, :show]
def index
@artists = Artist.all
end
def show
end
View quotes_controller.rb
class QuotesController < ApplicationController
before_action :set_quote, only: [:show]
before_action :require_login
skip_before_action :require_login, only: [:index, :show]
def show
end
def new
View gist:45609209623b147d388717af814ed4bf
class QuotesController < ApplicationController
before_action :set_quote, only: [:show]
before_action :require_login
skip_before_action :require_login, only: [:index, :show]
def show
end
def new
View Artist.rb
class Artist < ApplicationRecord
belongs_to :user
has_many :spoken_quotes, class_name: "Quote", foreign_key: :speaker_id
has_many :topic_quotes, class_name: "Quote", foreign_key: :topic_id
validates :user_id, presence: true
validates :name, presence: true, length: { maximum: 120 },
uniqueness: { case_sensitive: false }
end
View quote_form.html.erb
<%= form_for(@quote) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class='row'>
<div class='col'>
<div class='form-group'>
<%= f.label :speaker, 'Who said it?' %>
<%= f.collection_select :speaker_id, Artist.order(:name), :id, :name,
{prompt: 'Select an artist'}, {class: 'form-control select-artist'} %>
</div>