The article deletion function doesn't work. I tried to fix it but it didn't work.
Details of the source code can be found at https://github.com/chikashishirozu/tweet_app.
config/routes.rb
Rails.application.routes.draw do
devise_for :users
resources :patients
resources :posts
resources :destroy
root 'home#top'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
resources :tweets
get "about" => "home#about"
end
# app/views/posts/index.html.erb
<!-- app/views/posts/index.html.erb -->
<div class="main posts-index">
<h2 id="shinkiyuza02">投稿一覧</h2>
<div class="container">
<% @posts.each do |post| %>
<div class="posts-index-item">
<h3 class="post-title"><%= post.title %></h3> <!-- タイトルにクラスを追加 -->
<p class="post-content"><%= post.content %></p>
<p>作成日時: <%= post.created_at.in_time_zone('Tokyo').strftime('%Y年%m月%d日 %H:%M') %></p>
<p>編集日時: <%= post.updated_at.in_time_zone('Tokyo').strftime('%Y年%m月%d日 %H:%M') %></p>
<div class="button-group">
<%= link_to '編集', edit_post_path(post), class: 'my-button' %>
<%= link_to '削除', post, data: { turbo_method: :delete, confirm: '本当に削除しますか?' }, class: 'my-button-02' %>
</div>
</div>
<% end %>
<% flash.each do |message_type, message| %>
<div class="<%= message_type == 'notice' ? 'my-notice-class' : 'my-alert-class' %>">
<%= message %>
</div>
<% end %>
</div>
</div>
app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :set_post, only: [:edit, :update, :destroy, :show]
before_action :authorize_user!, only: [:edit, :update, :destroy]
def index
@posts = Post.order(created_at: :desc) # 投稿を新しい順に取得
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_params) # 現在のユーザーと紐付ける
if @post.save
redirect_to posts_path, notice: '投稿が作成されました'
else
render :new
end
end
def show
@post = Post.find_by(id: params[:id])
unless @post
flash[:alert] = "指定された投稿は存在しません。"
redirect_to root_path
end
end
def edit
# `set_post` が既に呼ばれているため、ここで再定義は不要
logger.debug "Edit action called with params: #{params.inspect}"
end
def update
if @post.update(post_params)
redirect_to posts_path, notice: '投稿が更新されました'
else
render :edit
end
end
def destroy
@post = Post.find(params[:id])
if @post.destroy
redirect_to posts_path, notice: '投稿が削除されました'
else
redirect_to posts_path, alert: '投稿の削除に失敗しました'
end
end
The delete button for posts doesn't work. I tried to fix it but it didn't work.
http://127.0.0.1:4003/posts/53 Post layout, button placement