snsアプリの投稿フォームを作っています。
投稿フォームに情報を入力して投稿しようとするとエラーが出てしまいます。
_micropost_form.html.erb フォームのパーシャル div class="container micropost-container"> <h1>Form</h1> <%= form_with(model: @micropost, url: microposts_path, local: true) do |form| %> <%= render 'shared/error_messages', object: form.object %> <div class="form-group"> <%= form.text_area :memo, class: 'form-control', placeholder: "メモを加えてください" %> </div> <div class="form-group"> <%= form.file_field :picture %> </div> <div class="form-group"> <%= form.submit "記録する", class: 'btn btn-info btn-lg form-submit' %> </div> <% end %> </div>
microposts/edit.html.erb <% provide(:title, 'メモ編集') %> <div class="container micropost-edit-container"> <div class="edit-titles"> <%= image_tag 'pencil.png', class: 'edit-img' %> <h1 class="title edit-micropost-title">メモ編集</h1> </div> <%= form_with(model: @micropost, url: micropost_path, local: true) do |form| %> <%= render 'shared/error_messages', object: form.object %> <div class="form-group"> <%= form.label :memo, 'メモ' %> <%= form.text_area :memo, class: 'form-control', placeholder: @micropost.memo %> </div> <div class="form-group"> <%= form.label :picture, '画像' %> <%= form.file_field :picture, class: 'form-control-file', placeholder: @micropost.picture %> </div> <div class="row"> <div class="col"> <div class="form-group"> <%= link_to "削除", micropost_path, method: :delete, data: { confirm: "本当に削除しますか?" }, class: 'btn btn-lg btn-danger btn-edit-user' %> </div> </div> <div class="col"> <div class="form-group"> <%= form.submit "編集", class: 'btn btn-info btn-lg btn-edit-user' %> </div> </div> </div> <div class="form-group"> <%= link_to "戻る", current_user, class: 'btn btn-lg btn-edit-user btn-back' %> </div> <% end %> </div>
microposts_controller.rb class MicropostsController < ApplicationController before_action :logged_in_user, only: [:create, :edit, :update, :destroy] before_action :correct_user, only: :destroy def create @user = current_user @micropost = current_user.microposts.build(micropost_params) if logged_in? @microposts = @user.microposts.page(params[:page]).per(10) if @micropost.save redirect_to current_user else render 'users/show' end end 中略 private def micropost_params params.require(:micropost).permit(:memo, :picture) end end
micropost_paramsでparams を許可しているのですが、
エラー文 ActionController::ParameterMissing in MicropostsController#create param is missing or the value is empty: micropost app/controllers/microposts_controller.rb:44:in `micropost_params' app/controllers/microposts_controller.rb:7:in `create'
となって読み取れていないようです。どなたか原因が分かる方はいらっしゃいませんか?
create時のログ Started POST "/microposts" for ::1 at 2019-12-16 21:43:29 +0900 Processing by MicropostsController#create as HTML Parameters: {"authenticity_token"=>"V0UoBbmVx9oKmvkJ4C1I2cDb4oxAPI0XIt/1Byuf18/jv/5xUHo09B33/dmsTJFDGGsfMQBoSAchMpXlep4/lw==", "memo"=>"ああああああ", "commit"=>"記録する"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:16:in `current_user' Completed 400 Bad Request in 2ms (ActiveRecord: 0.2ms | Allocations: 1202) ActionController::ParameterMissing (param is missing or the value is empty: micropost): app/controllers/microposts_controller.rb:45:in `micropost_params' app/controllers/microposts_controller.rb:7:in `create' Started GET "/microposts/148/edit" for ::1 at 2019-12-16 21:47:04 +0900 Processing by MicropostsController#edit as HTML Parameters: {"id"=>"148"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:16:in `current_user' Micropost Load (0.3ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = ? ORDER BY "microposts"."created_at" DESC LIMIT ? [["user_id", 1], ["id", 148], ["LIMIT", 1]] ↳ app/controllers/microposts_controller.rb:18:in `edit' Rendering microposts/edit.html.erb within layouts/application Rendered shared/_error_messages.html.erb (Duration: 0.3ms | Allocations: 75) Rendered microposts/edit.html.erb within layouts/application (Duration: 7.6ms | Allocations: 2045) [Webpacker] Everything's up-to-date. Nothing to do Rendered layouts/_header.html.erb (Duration: 3.0ms | Allocations: 1392) Rendered layouts/_footer.html.erb (Duration: 0.7ms | Allocations: 134) Completed 200 OK in 47ms (Views: 40.0ms | ActiveRecord: 0.5ms | Allocations: 14740) コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/16 12:53
2019/12/17 00:11
2019/12/17 00:14
2019/12/17 06:43
2019/12/17 06:46
2019/12/17 06:58
2019/12/17 07:08
2019/12/17 08:08
2019/12/17 08:10
2019/12/17 08:27
2019/12/17 08:46
2019/12/17 09:14
2019/12/17 12:01
2019/12/17 12:34
2019/12/17 12:40
2019/12/17 12:42
2019/12/17 14:21