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

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

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

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

Q&A

解決済

1回答

800閲覧

Rails 投稿機能実装したいが、投稿を行うとエラー画面になってしまう

yoshi32

総合スコア2

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/05/11 00:48

編集2023/05/11 22:09

実現したいこと

Rails 投稿機能実装したいが、投稿を行うとエラー画面になってしまう

前提

Railsで投稿機能実装しようとしていますが、投稿を行うとエラー画面になってしまいます。
初学者であり、質問的を得ていないかもしれませんが、何卒よろしくお願いいたします。

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

ActiveModel::UnknownAttributeError in PostsController#create unknown attribute 'post_user_id' for Post. Extracted source (around line #28): # POST /posts def create @post = current_user.posts.build(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else

該当のソースコード

rails

1class PostsController < ApplicationController 2 before_action :set_post, only: %i[show edit update destroy] 3 4 # GET /posts 5 def index 6 @posts = Post.all 7 end 8 9 # GET /posts/1 10 def show 11 @post = Post.find_by(params[:id]) 12 end 13 14 # GET /posts/new 15 def new 16 @post = Post.new 17 end 18 19 # GET /posts/1/edit 20 def edit 21 if @post.user_id != @current_user.id 22 @post = Post.find(params[:id]) 23 end 24 end 25 26# POST /posts 27 def create 28 @post = current_user.posts.build(post_params) 29 if @post.save 30 redirect_to @post, notice: 'Post was successfully created.' 31 else 32 render :new 33 end 34 end 35 36 # PATCH/PUT /posts/1 37 def update 38 @post = Post.find(params[:id]) 39 if @post.update(post_params) 40 redirect_to @post, notice: 'Post was successfully updated.' 41 else 42 render :edit 43 end 44 end 45 46 # DELETE /posts/1 47 def destroy 48 @post = Post.find(params[:id]) 49 if @post.user_id != @current_user.id 50 @post = Post.find(params[:id]) 51 @post.destroy 52 redirect_to @post, notice: 'Post was successfully destroyed.' 53 end 54 end 55 56 private 57 58 # Use callbacks to share common setup or constraints between actions. 59 def set_post 60 @post = Post.find(params[:id]) 61 end 62 63 # Only allow a trusted parameter "white list" through. 64 def post_params 65 params.require(:post).permit(:title, :content) 66 end 67end

post.rb

1class Post < ApplicationRecord 2 validates :title, presence: true, length: { maximum: 30 } 3 validates :content, presence: true, length: { maximum: 100} 4 5 belongs_to :user 6 def user 7 return User.find_by(id: self.user_id) 8 end 9 end

user.rb

1class User < ApplicationRecord 2 authenticates_with_sorcery! 3 4 validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] } 5 validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] } 6 validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] } 7 8 validates :email, uniqueness: true 9 validates :email, presence: true 10 validates :first_name, presence: true, length: { maximum: 255 } 11 validates :last_name, presence: true, length: { maximum: 255 } 12 13 has_many :posts, dependent: :destroy, foreign_key: :post_user_id 14end

schema

1# This file is auto-generated from the current state of the database. Instead 2# of editing this file, please use the migrations feature of Active Record to 3# incrementally modify your database, and then regenerate this schema definition. 4# 5# Note that this schema.rb definition is the authoritative source for your 6# database schema. If you need to create the application database on another 7# system, you should be using db:schema:load, not running all the migrations 8# from scratch. The latter is a flawed and unsustainable approach (the more migrations 9# you'll amass, the slower it'll run and the greater likelihood for issues). 10# 11# It's strongly recommended that you check this file into your version control system. 12 13ActiveRecord::Schema.define(version: 2023_05_07_041659) do 14 15 create_table "posts", force: :cascade do |t| 16 t.string "title" 17 t.text "content" 18 t.datetime "created_at", null: false 19 t.datetime "updated_at", null: false 20 end 21 22 create_table "users", force: :cascade do |t| 23 t.string "email", null: false 24 t.string "crypted_password" 25 t.string "salt" 26 t.string "first_name", null: false 27 t.string "last_name", null: false 28 t.datetime "created_at", null: false 29 t.datetime "updated_at", null: false 30 t.index ["email"], name: "index_users_on_email", unique: true 31 end 32 33end 34

試したこと

下記内容が原因ではないかとコードの一部修正を行ってみましたが、エラーは解消されませんでした。
・current_userがnilである場合に備えて、デフォルト値を設定する
・current_userがnilである場合には、ログイン画面にリダイレクトする
・editアクションとdestroyアクションの中で、@current_userをcurrent_userに修正する

class PostsController < ApplicationController before_action :set_post, only: %i[show edit update destroy] before_action :authenticate_user!, only: %i[new create edit update destroy] # GET /posts def index @posts = Post.all end # GET /posts/1 def show end # GET /posts/new def new @post = current_user.posts.build end # GET /posts/1/edit def edit if @post.user_id != current_user.id redirect_to @post, alert: "You don't have permission to edit this post." end end # POST /posts def create @post = current_user.posts.build(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else render :new end end # PATCH/PUT /posts/1 def update if @post.update(post_params) redirect_to @post, notice: 'Post was successfully updated.' else render :edit end end # DELETE /posts/1 def destroy if @post.user_id != current_user.id redirect_to @post, alert: "You don't have permission to delete this post." else @post.destroy redirect_to posts_url, notice: 'Post was successfully destroyed.' end end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a list of trusted parameters through. def post_params params.require(:post).permit(:title, :content) end end

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

エラー発生時のログは下記の通りです。

Started POST "/posts" for ::1 at 2023-05-11 09:47:22 +0900 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"F83JrNxipFNThAHJnUkTg9HOLjd1X4txR3FnJ3rCtAw23lRQp6gzEhib9mjv6zG6Lg0RItBWDS4yetJee79phA", "post"=>{"title"=>"test", "content"=>"test"}, "commit"=>"Create Post"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:28 Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms) ActiveModel::UnknownAttributeError (unknown attribute 'post_user_id' for Post.): app/controllers/posts_controller.rb:28:in `create'

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

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

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

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

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

winterboum

2023/05/11 01:40

model の定義がおかしそう。 post.rb, user.rb と schema 載せて下さい。
yoshi32

2023/05/11 03:26

ご返答ありがとうございます。 下記それぞれ記載いたします。(見辛くすみません) <post.rb> class Post < ApplicationRecord validates :title, presence: true, length: { maximum: 30 } validates :content, presence: true, length: { maximum: 100} belongs_to :user def user return User.find_by(id: self.user_id) end end <user.rb> class User < ApplicationRecord authenticates_with_sorcery! validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] } validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] } validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] } validates :email, uniqueness: true validates :email, presence: true validates :first_name, presence: true, length: { maximum: 255 } validates :last_name, presence: true, length: { maximum: 255 } has_many :posts, dependent: :destroy, foreign_key: :post_user_id end <schema> # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2023_05_07_041659) do create_table "posts", force: :cascade do |t| t.string "title" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", null: false t.string "crypted_password" t.string "salt" t.string "first_name", null: false t.string "last_name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true end end
winterboum

2023/05/11 05:44

code は質問欄を編集してそちらに書いてください。 インデントがないので読みづらい
yoshi32

2023/05/11 06:19

失礼いたしました。 ご指摘ありがとうございます、質問欄に追記いたしました。
guest

回答1

0

ベストアンサー

user.rb の has_many :posts, dependent: :destroy, foreign_key: :post_user_id の行に問題がありますね。
というヒントでとりあえずトライしてみてください。

投稿2023/05/11 07:48

winterboum

総合スコア23282

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

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

yoshi32

2023/05/11 07:49

ヒントありがとうございます!一旦トライしてみます!
yoshi32

2023/05/11 10:29

post.rbでユーザ定義されているため、 foreign_key: :post_user_id 部分不要かと思い削除してみましたが、エラー変わらずでした。 引き続きtryしてみますが、一旦状況共有まで!
winterboum

2023/05/11 10:35

「エラー変わらず」という表現は要注意。 相変わらずエラーがでるのは変わらず なのか 全く同じエラーがでる のか この表現だと後者と取られかねない。 全く同じですか? エラー がでた という現象は同じ でも、メッセージは変わってません?
yoshi32

2023/05/11 10:40

あ、仰る通りです、失礼しました!(勉強になります) エラーですが、 unknown attribute 'post_user_id' for Post. が unknown attribute 'user_id' for Post. に変わっていました!(他は一緒)
winterboum

2023/05/11 10:44

でしょうね。 そこが次のヒントです。
yoshi32

2023/05/11 13:09

ありがとうございます! belongs_to :userを記載し、db migrateしたら投稿できるようになりました! 引き続きdeleteとupdateがうまく反応していないので探索しますが、一旦投稿の方は解決できました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問