You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.0 KiB
62 lines
1.0 KiB
class ArticlesController < ApplicationController
|
|
|
|
before_action :authenticate_user! , only: [:new, :create]
|
|
|
|
def index
|
|
@articles = Article.all
|
|
end
|
|
|
|
def show
|
|
@article = Article.find(params[:id])
|
|
end
|
|
|
|
def new
|
|
@article = Article.new
|
|
end
|
|
|
|
def create
|
|
@article = Article.new(article_params)
|
|
|
|
if @article.save
|
|
redirect_to articles_path
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
|
|
def edit
|
|
@article = Article.find(params[:id])
|
|
end
|
|
|
|
def update
|
|
@article = Article.find(params[:id])
|
|
|
|
if @article.update(article_params)
|
|
redirect_to articles_path
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@article = Article.find(params[:id])
|
|
@article.destroy
|
|
|
|
redirect_to root_path, status: :see_other
|
|
end
|
|
|
|
|
|
def upvote
|
|
@article = Article.find(params[:id])
|
|
@article.votes.create
|
|
redirect_to @article
|
|
end
|
|
|
|
|
|
private
|
|
def article_params
|
|
params.require(:article).permit(:title, :body)
|
|
end
|
|
|
|
end
|
|
|