al13ntown/app/controllers/posts_controller.rb

83 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

2026-08-24 20:12:41 +00:00
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
allow_unauthenticated_access(only: [:index, :show])
#before_action :set_profile
before_action :set_blog
#before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
#@blog = Blog.all
@posts = Post.order(created_at: :desc)
end
def show
# @blog = Blog.find(params[:blog_id])
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
@post = Post.find(params[:id])
end
def new
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
@post = @blog.posts.build
end
def create
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
@post = @blog.posts.create(post_params)
if @post.save
redirect_to profile_blog_post_path(@profile, @blog, @post), notice: "Post created."
else
render :new, status: :unprocessable_entity
end
end
def edit
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
@post = @blog.posts.find(params[:id])
end
def update
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
@post = @blog.posts.find(params[:id])
if @post.update(post_params)
redirect_to profile_blog_post_path(@profile, @blog, @post), notice: "Post updated."
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@profile = Profile.find(params[:profile_id])
@blog = Blog.find(params[:blog_id])
#@post = @blog.posts.find(params[:id])
@post = Post.find(params[:id])
@post.destroy
redirect_to profile_blog_path(@profile, @blog), notice: "Post deleted."
end
private
def set_profile
@profile = profile.find(params[:profile_id])
end
def set_blog
@blog = Blog.find(params[:blog_id])
end
# def set_post
# @post = @blog.post.find(params[:id])
#end
def post_params
params.require(:post).permit(:title, :body, :text, :mood, :music, :icon_image)
end
end