質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

3238閲覧

Couldn't find Post without an IDのエラーが出る

coke-ko

総合スコア3

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/06/25 08:39

前提・実現したいこと

トップページに投稿記事に紐づく各ユーザーの写真を表示させたいです。

投稿(post)に紐づくユーザー写真を表示させようとしたところ以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

ActiveRecord::RecordNotFound in PostsController#index Couldn't find Post without an ID Extracted source (around line #6): 4 def index 5 @posts = Post.all.order(created_at: :desc) 6 @post = Post.find(params[:id]) 7 end

該当のソースコード

ruby

1(posts_controller.rb) 2class PostsController < ApplicationController 3 before_action :authenticate_user!, only: [:new, :create] 4 5 def index 6 @posts = Post.all.order(created_at: :desc) 7 @post = Post.find(params[:id]) 8 end 9 10 def new 11 @post = Post.new 12 end 13 14 def create 15 @post = Post.new(post_params) 16 if @post.save 17 redirect_to root_path 18 else 19 render :new 20 end 21 end 22 23 private 24 def post_params 25 params.require(:post).permit(:trip_date, :place, :title, :content).merge(user_id: current_user.id) 26 end 27end

ruby

1(posts/index.html.erb) 2<div class='post-contents'> 3 <div> 4 <h2 class='posts-contents-title'>新着情報</h2> 5 <ul class='post-lists'> 6 <% @posts.each do |post|%> 7 <li id="posts-top" class='list'> 8 <%#= link_to post_path(post.id) do %> 9 <div class='post-info'> 10 <p class='post-name'> 11 <%= post.trip_date %>/<%= post.place%> 12 </p> 13 <div class='post-price'> 14 <h3><%= post.title %></h3> 15 </div> 16 </div> 17 <div class= 'post-info-user'> 18 <%= image_tag @post.user_id.avatar, class: "post-image" if @post.user_id.avatar.attached? %> 19 <div class= 'user-info'> 20 <h3 class='user-info-name'>田中太郎</h3> 21 <p>投稿日 <%= post.created_at %></p> 22 </div> 23 </div> 24 <%# end %> 25 </li> 26 <% end %>

ruby

1(routes.rb) 2Rails.application.routes.draw do 3 devise_for :users 4 root to: 'posts#index' 5 resources :posts, only: [:index, :new, :create] 6 resources :users, only: [:show, :edit, :update] 7end 8

ruby

1(user.rb) 2class User < ApplicationRecord 3 has_many :posts 4 has_one_attached :avatar 5 # Include default devise modules. Others available are: 6 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 7 devise :database_authenticatable, :registerable, 8 :recoverable, :rememberable, :validatable 9 10 11 validates :name, presence: true 12end

試したこと

投稿記事が作成されてトップページに表示されたのは確認できたのですが、投稿したユーザーの写真の処理を加えたところ
Can't resolve image into URL: to_model delegated to attachment, but attachment is nil
が出てきたため(この処理は後ほど解決していきます)、インスタンス変数を作成したところ、postのidが見当たらないとエラーが出ました。
ポストの記事は表示されるのに、IDが見つからないのはなぜでしょうか?

どこに原因があるのか教えていただきたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

indexのコントローラーアクション内で@post = Post.find(params[:id])とすること自体が不要なのではないでしょうか?

投稿2021/06/25 09:00

編集2021/06/25 09:01
maisumakun

総合スコア145208

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

coke-ko

2021/06/25 09:33

ご回答ありがとうございます! @postsが表示されているからということでしょうか? Can't resolve image into URL: to_model delegated to attachment, but attachment is nil 上記の、投稿された記事にユーザー写真を載せようとするとエラーになるので今度はそれを解決していきたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問