al13ntown/app/controllers/blogs_controller.rb
2026-08-24 20:12:41 +00:00

70 lines
1.4 KiB
Ruby

class BlogsController < ApplicationController
#class BlogsController < ApplicationController
before_action :authenticate_user!, only: [:destroy, :new, :create, :edit, :update]
allow_unauthenticated_access(only: %i[index show])
before_action :set_profile
before_action :set_blog, only: [:show, :edit, :update, :destroy]
def index
@blogs = Blog.all
end
def edit
@blog = Blog.find(params[:id])
end
def new
@blog = @profile.blogs.new
@blog = @profile.blogs.build
end
def create
@blog = @profile.blogs.new(blog_params)
if @blog.save
redirect_to root_path, notice: 'Blog was successfully created.'
else
render :new, notice: 'Could not save blog.'
end
end
def destroy
@profile = Profile.find(params[:profile_id])
@profile = @profile.blogs.find(params[:id])
@blog.destroy
redirect_to profile_path(@profile), notice: "Blog deleted."
end
def show
@profile = Profile.find(params[:profile_id])
@blog = @profile.blogs.find(params[:id])
@posts = @blog.posts.order(created_at: :desc)
end
def update
@blog = Blog.find(params[:id])
if @blog.update(blog_params)
redirect_to profile_blog_path(@profile)
else
render :edit
end
end
end
private
def blog_params
params.require(:blog).permit(:title, :topic)
end
def set_profile
@profile = Profile.find(params[:profile_id])
end
def set_blog
@blog = @profile.blogs.find(params[:id])
end