The page you were looking for doesn't exist.
herokuの本番環境でフィード投稿の削除ができるようにしたい。
railsチュートリアルで、herokuにデプロイした後、本番環境で投稿した内容を削除しようとすると、タイトルのようなエラーが発生します。
発生している問題・エラー
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
If you are the application owner check the logs for more information.
確認したソースコード
内容などはほぼチュートリアルの通りにしているはずです。
下記に記載しているコードを確認・修正してデプロイし直しても変わらなかったです。
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, omly: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
<span class="content">
<%= micropost.content %>
<%= image_tag micropost.display_image if micropost.image.attached? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
@micropost.image.attach(params[:micropost][:image])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
#redirect_back(fallback_location: root_url)
end
private
def micropost_params
params.require(:micropost).permit(:content, :image)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
自分で試したこと
heroku logs --tailコマンドでherokuのログを確認してみたら、下記の内容が表示されました。
(省略)
2022-07-07T21:17:23.363913+00:00 heroku[router]: at=info method=GET path="/microposts/304" host=pure-shore-72426.herokuapp.com request_id=e43ab7da-b5d7-47e2-8c91-49ec62d270de fwd="157.107.74.3" dyno=web.1 connect=0ms service=2ms status=404 bytes=1966 protocol=https
(省略)