質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

961閲覧

Ruby ActionController::ParameterMissing in PostsController#createのエラー解決

ryota1107

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2021/03/06 08:19

編集2021/03/07 23:19

前提・実現したいこと

Rails初心者です。
Rails6で、フォームを作る際、以下のエラーが出て困っております。
どのようにすれば、エラーを解消できるでしょうか?

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

ActionController::ParameterMissing in PostsController#create param is missing or the value is empty: posts

イメージ説明

該当のソースコード

Ruby

1class PostsController < ApplicationController 2 def index 3 @post = Post.all 4 end 5 def new 6 @post = Post.new 7 end 8 def create 9 @post = Post.new(posts_params) 10 if @post.save 11 redirect_to root_path 12 else 13 render :new 14 end 15 end 16 private 17 def posts_params 18 params.require(:post).permit(:outer,:tops,:pants,:shoes,:hat,:accessory,:season_id,images:[]).merge(user_id: current_user.id) 19 end 20end

Ruby

1<div class="post-sell-contents"> 2 3 4 <div class="post-sell-main"> 5 <h2 class="post-sell-title">コーデディネイトの情報を入力</h2> 6 <%= form_with model:@post,url:posts_path,local: true do |f| %> 7 8 9 <%= render 'shared/error_messages', model: f.object%> 10 11 12 <%# 投稿画像 %> 13 <div class="img-upload"> 14 15 <div class="weight-bold-text"> 16 コーディネイト画像 17 <span class="indispensable">必須</span> 18 </div> 19 <div class="click-upload"> 20 <p> 21 クリックしてファイルをアップロード 22 </p> 23 <%= f.file_field :images,name: 'post[image][]', id:"post-image" %> 24 <div id="image-list"></div> 25 26 </div> 27 </div> 28 29 <%# /投稿画像 %> 30 <%# アイテム名 %> 31 <div class="new-items"> 32 <div class="weight-bold-text"> 33 アウター 34 35 </div> 36 <%= f.text_area :outer, class:"post-text", id:"post-name", placeholder:"アウター名", maxlength:"20" %> 37 <div class="new-items"> 38 <div class="weight-bold-text"> 39 トップス 40 <span class="indispensable">必須</span> 41 </div> 42 <%= f.text_area :tops, class:"post-text", id:"post-name", placeholder:"トップス名", maxlength:"20" %> 43 <div class="new-items"> 44 <div class="weight-bold-text"> 45 パンツ 46 <span class="indispensable">必須</span> 47 </div> 48 <%= f.text_area :pants, class:"post-text", id:"post-name", placeholder:"パンツ名", maxlength:"20" %> 49 <div class="new-items"> 50 <div class="weight-bold-text"> 5152 <span class="indispensable">必須</span> 53 </div> 54 <%= f.text_area :shoes, class:"post-text", id:"post-name", placeholder:"靴名", maxlength:"20" %> 55 <div class="new-items"> 56 <div class="weight-bold-text"> 57 帽子 58 </div> 59 <%= f.text_area :hat, class:"post-text", id:"post-name", placeholder:"帽子名", maxlength:"20" %> 60 <div class="new-items"> 61 <div class="weight-bold-text"> 62 アクセサリー 63 </div> 64 <%= f.text_area :accessory, class:"post-text", id:"post-name", placeholder:"アクセサリー名", maxlength:"20" %> 65 <div class='new-items'> 66 <div class="weight-bold-text"> 67 季節 68 <span class="indispensable">必須</span> 69 </div> 70 <%= f.collection_select(:season_id,Season.all,:id,:name,{},{class:"post-text", id:"post-season-fee-status"}) %> 71 </div> 72 73 74 <%# /配送について %> 75 76 77 78 <%# 下部ボタン %> 79 <div class="sell-btn-contents"> 80 <%= f.submit "投稿する" ,class:"sell-btn",id: "subit" %> 81 <%=link_to 'もどる', root_path, class:"back-btn" %> 82 </div> 83 <%# /下部ボタン %> 84 85 <% end %> 86 </div> 87</div>

javascript

1if (document.URL.match( /new/ ) ) { 2 document.addEventListener('DOMContentLoaded', function(){ 3 const ImageList = document.getElementById('image-list'); 4 5 6 const createImageHTML = (blob) => { 7 const imageElement = document.createElement('div'); 8 imageElement.setAttribute('class', "image-element") 9 let imageElementNum = document.querySelectorAll('.image-element').length 10 11 12 13 const blobImage = document.createElement('img'); 14 15 blobImage.setAttribute('src', blob); 16 17 const inputHTML = document.createElement('input') 18 inputHTML.setAttribute('id', `post_image_${imageElementNum}`) 19 inputHTML.setAttribute('name', 'post[images][]') 20 inputHTML.setAttribute('type', 'file') 21 22 23 imageElement.appendChild(blobImage); 24 imageElement.appendChild(inputHTML) 25 ImageList.appendChild(imageElement); 26 inputHTML.addEventListener('change', (e) => { 27 file = e.target.files[0]; 28 blob = window.URL.createObjectURL(file); 29 30 createImageHTML(blob) 31 }) 32 }; 33 34 document.getElementById('post-image').addEventListener('change', function(e){ 35 36 37 38 const file = e.target.files[0]; 39 const blob = window.URL.createObjectURL(file); 40 41 createImageHTML(blob); 42 }); 43 }); 44}

試したこと

コンソールで@postsの中身を確認
@postに変えてみたがダメでした

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

ご教授お願いいたします!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

別の質問
と同じ原因です

追記
単数形と複数形がごちゃごちゃですね。
newやeditでは扱うのは一つですから、posts、@posts、:posts などみな単数形にしてみて。
Railsは複数形でも大丈夫なところが多いですが、単数形でなければならない所もあるので、それが原因になってます

投稿2021/03/06 08:55

編集2021/03/06 10:05
winterboum

総合スコア23376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ryota1107

2021/03/06 09:28

一度消して記述し直しまたがエラー解決できません。
ryota1107

2021/03/06 09:36

@postsがnullと言うところまではわかったのですが、 ご教授ください。 よろしくお願いします。
ryota1107

2021/03/06 10:42

ご連絡ありがとうございます。 ビューのクラス名以外はpostsに直してみましたがエラー解決できませんでした。 またご教授いただけると幸いです。
winterboum

2021/03/06 11:53

どう直したのか載せて え? 複数形にしたの?単数形にしろって書いただろ
ryota1107

2021/03/07 23:20

修正箇所載せずにコメントしてしまい申し訳ありません。 先程修正箇所載せました。
ryota1107

2021/03/07 23:21

サーバー再起動したらなおりました。 ありがとうございます。 また修正箇所の不備申し訳ありませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問