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

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

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

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

Ruby on Rails

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

Q&A

1回答

7748閲覧

ruby rails エラー:SQLite3::ConstraintException: NOT NULL constraint failed:

atamawaruidesu

総合スコア0

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/07/19 08:19

編集2020/07/19 09:24

前提・実現したいこと

railsで掲示板サービスを制作しています。
アカウント登録したユーザーがトピックスを作ったり、参加したりして、投稿フォームに記述した物をトピックに表示するようにしたいです。
今回のエラーはトピックを作成しトピックのトップページに移動し投稿フォーム画面へ移動しテキストを入力して投稿ボタンを押すとエラーが出てきます。

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

ActiveRecord::NotNullViolation in PostsController#create SQLite3::ConstraintException: NOT NULL constraint failed: posts.topic_id

該当のソースコード

class PostsController < ApplicationController before_action :authenticate_user! before_action :find_post,only: [:show, :edit, :update, :destroy] def index @post = Post.all end def show @topic = Topic.find(params[:id]) @posts = Post.where(topic_id: params[:id]) end def new return redirect_to new_profile_path,alert: "プロフィールを登録してください" if current_user.profile.blank? @post = Post.new end def create @post = Post.new(post_params) @post.user = current_user if @post.save redirect_to root_path, notice: "投稿に成功しました" else render :new end end def update if @post.update(post_params) redirect_to root_path, notice: "投稿を更新しました" else render :edit end end def destroy if @post.destroy redirect_to root_path, notice: "投稿を削除しました" else redirect_to root_path, alert: "投稿を削除できませんでした" end end private def post_params params.require(:post).permit( :content ) end def find_post @post = Post.find(params[:id]) end end
class TopicsController < ApplicationController def index @topics = Topic.all @newTopic = Topic.new end def show @topic = Topic.find(params[:id]) @posts = Post.where(topic_id: params[:id]) end def create @topic = Topic.new(params[:topic].permit(:title)) @topic.save redirect_to topics_index_path end def delete @topic = Topic.find(params[:id]) @topic.destroy redirect_to topics_index_path end end
create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false t.integer "record_id", null: false t.integer "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end create_table "active_storage_blobs", force: :cascade do |t| t.string "key", null: false t.string "filename", null: false t.string "content_type" t.text "metadata" t.bigint "byte_size", null: false t.string "checksum", null: false t.datetime "created_at", null: false t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end create_table "group_users", force: :cascade do |t| t.integer "group_id" t.integer "user_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["group_id"], name: "index_group_users_on_group_id" t.index ["user_id"], name: "index_group_users_on_user_id" end create_table "groups", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["name"], name: "index_groups_on_name", unique: true end create_table "posts", force: :cascade do |t| t.text "content" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.integer "topic_id", null: false t.index ["topic_id"], name: "index_posts_on_topic_id" t.index ["user_id"], name: "index_posts_on_user_id" end create_table "profiles", force: :cascade do |t| t.integer "user_id", null: false t.string "name" t.text "career" t.string "image" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["user_id"], name: "index_profiles_on_user_id" end create_table "topics", force: :cascade do |t| t.string "title" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "topic_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "group_users", "groups" add_foreign_key "group_users", "users" add_foreign_key "posts", "topics" add_foreign_key "posts", "users" add_foreign_key "profiles", "users" end

試したこと

このエラーについて検索しましたが類似の記事も少なくわかりません。
勉強を始めてから1週間なので説明が下手くそで分かり難いとは思いますが、どうかよろしくお願いします。

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

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

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

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

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

guest

回答1

0

Postをcreate(掲示板に投稿する)ときに、Topicと紐付いていないために発生しているエラーになります。

PostsControllerpost_params メソッドを

diff

1 def post_params 2 params.require(:post).permit( 3- :content 4+ :content, 5+ :topic_id 6 ) 7 end

として、topic_idを指定できるようにすると動くかと思います。

投稿2020/07/21 09:29

ducci

総合スコア191

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問