RBOnRails-learning/app/controllers/articles_controller.rb

53 lines
963 B
Ruby
Raw Permalink Normal View History

2024-08-07 14:43:33 +00:00
class ArticlesController < ApplicationController
2024-08-08 09:19:44 +00:00
before_action :require_user, except: [:show, :index]
2024-08-07 14:43:33 +00:00
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)
2024-08-08 09:19:44 +00:00
@article.user_id = current_user.id
2024-08-07 14:43:33 +00:00
if @article.save
redirect_to @article
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 @article
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
private
def article_params
params.require(:article).permit(:title, :body, :status)
end
end