実現したいこと
DBに保存できるように実装したい。
発生している問題・分からないこと
commentモデルのテーブルに保存されない。
エラーメッセージ
error
1Started POST "/users" for ::1 at 2024-01-25 00:17:03 +0900 2Processing by Devise::RegistrationsController#create as HTML 3 Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f54550c9080 @tempfile=#<Tempfile:/tmp/RackMultipart20240125-70344-4klztr.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)"=>"1947", "birthdate(2i)"=>"1", "birthdate(3i)"=>"18", "message"=>"頑張る"}, "commit"=>"SEND"} 4 User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 5Redirected to http://localhost:3000/ 6Filter chain halted as :require_no_authentication rendered or redirected 7Completed 302 Found in 7ms (ActiveRecord: 1.0ms | Allocations: 1806)
該当のソースコード
user_controller
1 (一部抜粋) 2def create 3 @comment = Comment.new(comment_params) 4 puts current_user 5 if @comment.save 6 redirect_to users_path 7 else 8 redirect_to new_comment_path, alert: 'コメントの保存に失敗しました。' 9 end 10 end 11 def show 12 @user = User.find(params[:id]) 13 end 14 15 private 16 def comment_params 17 params.require(:comment).permit(:birthdate, :strengths, :weaknesses, :image, :message).merge(user_id: current_user.id) 18 end 19end 20 21
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
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
<%= form_with model: @user, url: users_path, id: 'charge-form', class: 'transaction-form-wrap', data: { turbo: false }, local: true do |f| %> <table class="detail-table"> <tbody> <% @users.each do |user| %> <tr> <th class="detail-user">名前</th> <td class="detail-value"><%= user.name %></td> </tr> <tr> <th class="detail-user">ニックネーム</th> <td class="detail-value"><%= user.nickname %></td> </tr> <% end %> </tbody> </table> <% @comments.each do |comment| %> <table class="detail-table"> <tbody> <tr> <th class="detail-user">生年月日</th> <td class="detail-value"><%= comment.birthdate %></td> </tr> <tr> <th class="detail-user">強み</th> <td class="detail-value"><%= comment.strengths %></td> </tr> <tr> <th class="detail-user">弱み</th> <td class="detail-value"><%= comment.weaknesses %></td> </tr> <tr> <th class="detail-user">メッセージ</th> <td class="detail-value"><%= comment.message %></td> </tr> </tbody> </table> <div class='user-photo-info'> <% if comment.image.attached? %> <%= image_tag comment.image, class: 'user-photo-img' %> <% else %> <%= image_tag 'test_image.jpg.jpg', class: 'user-photo-img', alt: 'Default Image' %> <% end %> </div> <% end %> <% end %>
application_controller
1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 private 5 def configure_permitted_parameters 6 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname,:name]) 7 end 8end
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found
chatGPTで上記のエラー内容を調べ、require_no_authenticationが影響してcreateアクションに入る前にリダイレクトしていることがわかりました。そこでbeforeアクションをコメントアウトしたり、トラゼクション、パラメーターを調べましたが原因はわかりませんでした。
補足
rails 7.0.0 を使用しています。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/01/30 13:46