ここに質問の内容を詳しく書いてください。
Ruby, Ruby on Rails, MySQLで画像とメッセージの投稿機能がついたwebページを作成しています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
ブラウザ上にはエラーメッセージはなくローカルサーバー上にrollbackとなっている状態です。
Processing by MainController#create as HTML Parameters: {"authenticity_token"=>"ywrKkEga226k/xli+vNZCiYsqtLtZHaJ7X+xdkBlHk4qdWqG3gOB+E7nd9zLKKcHYkaYCr3BrF6PuQIqA1LgIA==", "post"=>{"post_title"=>"test", "post_text"=>"test"}, "commit"=>"投稿する"} User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 (0.2ms) BEGIN ↳ app/controllers/main_controller.rb:13:in `create' User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 ↳ app/controllers/main_controller.rb:13:in `create' (0.2ms) ROLLBACK ↳ app/controllers/main_controller.rb:13:in `create' Rendering main/new.html.erb within layouts/application Rendered shared/_second-header.html.erb (Duration: 1.0ms | Allocations: 685) Rendered shared/_second-footer.html.erb (Duration: 0.2ms | Allocations: 331) Rendered main/new.html.erb within layouts/application (Duration: 2.0ms | Allocations: 1528) [Webpacker] Everything's up-to-date. Nothing to do
該当のソースコード
Maincontroller
def index end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to main_index_path else render 'new' end end private def post_params params.require(:post).permit(:image, :post_title, :post_text).merge(user_id: current_user.id) end end
Post model
class Post < ApplicationRecord belongs_to :user has_one_attached :image with_options presence: true do validates :image validates :post_title validates :post_text end end
new.html.erb
<label class="form-image"> <div class="image-file"> <span>画像を選択</span> <input type="file" class="hidden"> <%= f.file_field :image, class:"hidden" %> </div> </label>
試したこと
・brew install imagemagickの実行
・下記Gemをgemfileに記述し、bundleインストールのし直し
gem 'mini_magick'
gem 'image_processing', '~> 1.2'
・ローカルサーバーの再起動
・rails active_storage:install
・rails db:migrate
・binding.pryでparamsを確認
>画像データがparamsに入っておらず、画像データのみ受け取りに失敗している状態を確認
・画像以外のデータがカラム保存できるか確認の為、モデルに記述したimageのバリデーション設定をコメントアウトし、タイトルとテキストだけを入力し保存を試した所、カラムに保存されていました。
全てできることは行ったつもりなのですが、何か見落としているものがないか、ご教授頂けますと幸いです。
どうぞ宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。