RubyonRails 投稿機能でのエラーの直し方

前提

投稿機能を作成しています。
データをデータベースに保存するためのsaveメソッドで発生したエラーが解決できません。

実現したいこと

エラーの発生した原因と解決法

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

ActiveRecord::NotNullViolation in Public::ContributionsController#create SQLite3::ConstraintException: NOT NULL constraint failed: contributions.user_id Extracted source (around line #21): def create @contribution =Contribution.new(contribution_params) @contribution.save redirect_to contributions_path end

該当のソースコード

resources :contributions, only: [:new, :index, :show, :edit, :update] post "contributions" => "contributions#create"
<%= form_with model: @contribution do |f| %> <h3>タイトル</h3> <%= f.text_field :title %> <h3>本文</h3> <%= f.text_area :text %> <%= f.submit '投稿' %> <% end %>
class Public::ContributionsController < ApplicationController def new @contribution = Contribution.new end def index @contribution = Contribution.all end def show end def edit end def update end def create @contribution = Contribution.new(contribution_params) @contribution.save redirect_to contributions_path end private def contribution_params params.require(:contribution).permit(:title, :text) end end

試したこと

routesやcontrollerの定義などを変えて色々試してみたのですが解決できなくて
質問させていただきました!
ご回答いただけると幸いです。

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

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント