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

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

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

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

Q&A

解決済

1回答

456閲覧

投稿内容が表示されない

koikoikoikoi

総合スコア14

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/02/09 07:54

編集2023/02/09 08:15

実現したいこと

投稿内容を表示させたい

前提

簡単なSNSサイトを作っているのですが投稿内容が表示されず行き詰まっています。
投稿詳細ページに遷移した際、以下のエラーが発生してしまいます。

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

ActiveRecord::UnknownPrimaryKey in Public::PostsController#show Unknown primary key for table posts in model Post. Extracted source (around line #8): def show @post = Post.find(params[:id]) ←エラー該当箇所 end

該当のソースコード

/app/views/public/posts/show.html.erb <h1>投稿詳細</h1> <table style="table"> <tr> <th> <p>店名</p> </th> <td> <p><%= @post.store_name %></p> </td> </tr> <tr> <th> <p>郵便番号</p> </th> <td> <p><%= @post.post_code %></p> </td> </tr> <tr> <th> <p>住所</p> </th> <td> <p><%= @post.address %></p> </td> </tr> <tr> <th> <p>住所</p> </th> <td> <p><%= @post.address %></p> </td> </tr> </table> <h3><%= @user.last_name %> <%= @user.first_name %>さんの投稿一覧</h3> 作成中
/app/controllers/public/posts_controller.rb class Public::PostsController < ApplicationController def index @posts = Post.all #@post = @post.id end def show @post = Post.find(params[:id]) end def new @post = Post.new end def edit end def create @post = Post.new(post_params) @post.user_id = current_user.id # binding.pry @post.save redirect_to posts_path end private def post_params params.require(:post).permit(:store_name, :post_code, :address, :regular_holiday, :review) end end
/app/models/user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable def self.guest find_or_create_by!(email: 'guest@example.com') do |user| user.password = SecureRandom.urlsafe_base64 # user.confirmed_at = Time.now # Confirmable を使用している場合は必要 # 例えば name を入力必須としているならば, user.name = "ゲスト" なども必要 end end has_many :posts, dependent: :destroy has_many :favorites, dependent: :destroy has_many :post_comments, dependent: :destroy #フォローした、されたの関係 has_many :user_relationships, class_name: "UserRelationship", foreign_key: "follower_id", dependent: :destroy has_many :reverse_of_user_relationships, class_name: "UserRelationship", foreign_key: "following_id", dependent: :destroy #一覧画面で使う has_many :followings, through: :user_relationships, source: :following has_many :followers, through: :reverse_of_user_relationships, source: :follower #フォローした時の処理 def follow(user_id) user_relationships.create(following_id: user_id) end #フォローを外す時の処理 def unfollow(user_id) user_relationships.find_by(following_id: user_id).destroy end #フォローしているか判定 def following?(user) followings.include?(user) end end
/app/models/post.rb class Post < ApplicationRecord has_one_attached :image has_many :favorites has_many :comments has_many :post_genres has_many :genres, through: :post_genres has_many :favorites, dependent: :destroy belongs_to :user has_many :post_comments, dependent: :destroy def favorited?(user) favorites.where(user_id: user.id).exists? end end
/db/migrate/20230206055945_create_posts.rb class CreatePosts < ActiveRecord::Migration[6.1] def change create_table :posts do |t| # t.integer :id t.integer :user_id t.string :store_name t.string :post_code t.string :address t.string :regular_holiday t.string :review t.timestamps end end end
/db/schema.rb create_table "posts", id: false, force: :cascade do |t| t.integer "user_id", null: false t.string "store_name", null: false t.string "post_code", null: false t.string "address", null: false t.string "regular_holiday", null: false t.string "review", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end

試したこと

そもそもUnknown primary key for tableのエラーを検索してもほとんどヒットしませんでした。
ヒットしたのは複合主キーを扱っているサイトばかりで自分の場合、複合主キーは設定していないので当てはまりません。今回のエラーについて有用そうな記事が全く見つけられなかったので質問させていただきました。
投稿一覧のページでも同様のエラーが出てます。postをテーブル名にしてしまったのが原因なのかと思いましたがカラム名を変えるのも大変そうなので躊躇いました。

補足情報(FW/ツールのバージョンなど)

Rails 6.1.7.2

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

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

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

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

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

winterboum

2023/02/09 08:07

model Post の DB migration file と、db/schema.rb の posts のところを載せてください
winterboum

2023/02/09 08:09

ああ、 あと、Public::PostsController#show を呼び出している view のcodeも
koikoikoikoi

2023/02/09 08:16

追記しましたm(__)m よろしくお願いいたします。
guest

回答1

0

ベストアンサー

create_table "posts", id: false, force: :cascade
とあり、id が定義されていないですね。
class CreatePosts ではそういう記述が見えないので はて? ですが、もしかして db:migrate したあとで class CreatePosts を編集していませんか?

投稿2023/02/09 08:19

編集2023/02/09 08:23
winterboum

総合スコア23464

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

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

koikoikoikoi

2023/02/09 08:37 編集

create_posts.rbにコメントアウトしている t.integer :idのカラムを最初に間違えて作成してしまったのでremove_id_from_posts.rbを作って消しました。その結果、意図しない形でid: falseになってしまったかもしれません。 とりあえずid:falseを直した方が良いですか??
winterboum

2023/02/09 08:40

Postのデータは削除不能ですか? もし削除しても大丈夫なら drop table して create table し直したほうがよいかも。 primary key としての id は自動的にinclimentされますが、ad_columnで作ったものはそうはならないと思うので。
koikoikoikoi

2023/02/09 12:11

postテーブル作り直したら解決しました!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.41%

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

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

質問する

関連した質問