@okmon1222

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

DBに保存できるようにしたい

Q&A

Closed

解決したいこと

Ruby on Railsで簡素なWebアプリをつくっています。
rails7.0.0を使用しています。commentテーブルに保存できるようにしたい。

発生している問題・エラー

Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 0.5ms | Allocations: 1562)

該当するソースコード

コントローラー(該当箇所のみ)
 def create   
    @comment = Comment.new(comment_params)
    if @comment.save
      redirect_to users_path
    else
      render :new, status: :unprocessable_entity
   end
  end

  private
  def comment_params
    params.require(:comment).permit(:birthdate, :strengths, :weaknesses, :image, :message).merge(user_id: current_user.id)ソースコード

モデル
class Comment < ApplicationRecord
  has_one_attached :image
  validates :image, presence: true
  validates :strengths, presence: true
  validates :weaknesses, presence: true
  validates :message, presence: true
  belongs_to :user
end

マイグレーションファイル
class CreateComments < ActiveRecord::Migration[7.0]
  def change
    create_table :comments do |t|
      t.text :message, null: false
      t.date :birthdate, null: false
      t.text :strengths, null: false
      t.text :weaknesses, null: false
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end
  end
end

ブラウザ上で遷移した際のログ(該当箇所のみ抜粋)
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f1970e14610 @tempfile=#<Tempfile:/tmp/RackMultipart20240124-34762-ggewrf.jpg>, @content_type="image/jpeg", @original_filename="staff1.jpg", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"staff1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "strengths"=>"根性", "weaknesses"=>"器用貧乏", "birthdate(1i)"=>"1992", "birthdate(2i)"=>"9", "birthdate(3i)"=>"18", "message"=>"gannbare"}, "commit"=>"SEND"}
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1
Redirected to http://localhost:3000/

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。
・トラザクションの検証
・binding.pryを使用してのデバック (createアクションに記述したが反応しなかった)
・パラメーターの確認
・バリデーションの確認
・ログの確認
・ブラウザ上でのエラーの有無>
・require_no_authenticationを調べ、before_actionをコメントアウトしたり、ログアウトしてログインしなおした。

0 likes

No Answers yet.

Your answer might help someone💌