解決したいこと
バリデーションを導入した際に引数のエラーが出てしまったので解決したいです。
ArgumentError in GogakusController#create wrong number of arguments (given 3, expected 1..2) @gogaku = current_user.gogakus.build(gogaku_params) byebug if @gogaku.save <=ここでエラーが発生 flash[:success] = "登録完了!" redirect_to root_url else Parameters: {"authenticity_token"=>"[FILTERED]", "gogaku"=> {"subject"=>"p", "body"=>"p", "file"=> #<ActionDispatch::Http::UploadedFile:0x00007fd18905c7a8 @content_type="audio/mpeg", @headers="Content-Disposition: form-data; name=\"gogaku[file]\"; filename=\"167_Unit 11.5_natural.mp3\"\r\n" + "Content-Type: audio/mpeg\r\n", @original_filename="167_Unit 11.5_natural.mp3", @tempfile=#<File:/tmp/RackMultipart20220318-1-e7ee00.mp3>>, "answer"=>""}, "commit"=>"登録"}
処理の流れ
newアクションでフォームを表示してからcreateアクションで新しくデータを作成する直後にこのエラーが発生しました。下のコードは「gem 'active_storage_validations', '0.8.2'」を使用する際に記述しました。もともと(バリデーションをかけないとき)はこのエラーは発生せず、きちんとsaveが実行されていました。追加したバリデーションはこちらになります。ちなみにこのgemを追加しただけでバージョンをかけなければ正常に動作します。
# app/models/gogaku.rb validates :file,size: { less_than: 1.megabytes, message: "should be less than 1MB" }
controller
def new @gogaku = Gogaku.new end def create @gogaku = current_user.gogakus.build(gogaku_params) # @gogaku.file = params[:gogaku][:file] #byebug if @gogaku.save flash[:success] = "登録完了!" redirect_to root_url else @feed_items = current_user.feed.paginate(page: params[:page]) flash[:danger] = @gogaku.errors.full_messages if @gogaku.errors.any? redirect_to new_gogaku_path end def gogaku_params params.require(:gogaku).permit(:subject, :body, :file, :answer,) end
new.html.erb *一応コメントアウトしたjqueryの処理付き
<h1>教材登録</h1> <% if flash.any? %> <% flash.each do |message_type, messages| %> <% messages.each do |message| %> <div class="alert alert-<%= message_type %>"><%= message %></div> <% end %> <% end %> <% end %> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <%= form_with model: @gogaku do |f| %> <%# ,name: "GogakuForm",id:"G_id" %> <div class = "form-group"> <label>教科</label> <%= f.text_field :subject, class:"form-control"%> </div> <div class = "form-group"> <label>内容</label> <%= f.text_field :body, class:"form-control"%> </div> <div class = "form-group"> <label>音声</label> <span class="file"> <%= f.file_field :file,accept:"audio/*",class:"form-control" %> <%# <script> const maxFileSize=5242880 //アップロードできる最大サイズを指定(1048576=1MB) $("input[type=file]").change(function(){ //ファイルがアップロードされたら $(".error_msg").remove() //エラーメッセージ削除 let uploaded_file=$(this).prop('files')[0]; //アップロードファイル取得 if(maxFileSize < uploaded_file.size){ //もし上限値を超えた場合 $(this).val("") //画像を空にする $(this).after("<p class='error_msg'>アップロードできる画像の最大サイズは5MBです</p>") //エラーメッセージ表示 } }) </script> ,name:"image[]" %> </span> </div> <%# ,id:"audio_file", type:"file" ,accept:"audio/*", name:"title" %> <div class = "form-group"> <label>答え</label> <%= f.text_field :answer, class:"form-control"%> </div> <% if current_user.gogakus.count < 5 %> <%= f.submit"登録",class:"btn btn-danger"%> <% else %> <h2>これ以上音声を登録出来ません。今ある音声を削除すれば新しく登録出来ます。</h2> <% end %> <%= link_to "戻る", root_url ,class:"btn btn-info"%> <% end %>
エラーが発生する前のparams状態をbeybugで知らべました
(byebug) params #<ActionController::Parameters {"authenticity_token"=>"#一応伏せます", "gogaku"=>#<ActionController::Parameters {"subject"=>"try", "body"=>"try", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007fd18b4c1aa8 @tempfile=#<Tempfile:/tmp/RackMultipart20220318-1-r9b6b9.mp3>, @original_filename="164_Unit 11.4_slow.mp3", @content_type="audio/mpeg", @headers="Content-Disposition: form-data; name=\"gogaku[file]\"; filename=\"164_Unit 11.4_slow.mp3\"\r\nContent-Type: audio/mpeg\r\n">, "answer"=>""} permitted: false>, "commit"=>"登録", "controller"=>"gogakus", "action"=>"create"} permitted: false>
一応エラーが発生した時のものも載せます
(byebug) params #<ActionController::Parameters {"authenticity_token"=>"#一応伏せます", "gogaku"=>#<ActionController::Parameters {"subject"=>"try", "body"=>"try", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007f8ea00628c8 @tempfile=#<Tempfile:/tmp/RackMultipart20220318-1-oae2z1.mp3>, @original_filename="164_Unit 11.4_slow.mp3", @content_type="audio/mpeg", @headers="Content-Disposition: form-data; name=\"gogaku[file]\"; filename=\"164_Unit 11.4_slow.mp3\"\r\nContent-Type: audio/mpeg\r\n">, "answer"=>""} permitted: false>, "commit"=>"登録", "controller"=>"gogakus", "action"=>"create"} permitted: false>
見てわかる通りエラーが発生してもしなくてもparamsの中身はほぼ変わっていませんでした。
自分で試したことなど
・beybugを使って自分なりに知らべましたが、なぜparamsの中身が同じなのにgemでバリデーションをかけると引数のエラーになってしまうのか分かりません。
・ほかの方法(jqueryを使う方法)でファイルサイズのチェックを実装してみましたが、form_withを使う都合上、処理でname属性を使ってしまうと上手く行かずエラーになることが分かりました(登録はできるが、showアクションでなぜかエラーが出てしまう)。
・そもそもこの時点でエラーが2つ{MIMEのエラーと一部の保存したファイル(条件がまだ特定できていませんが、500エラーかつ時間は関係なさそうだったので、恐らくActiveStrageのエラー)が読み込めないでエラーになる}発生しているのでそれが原因かもしれません。私のこのエラーに関する過去の質問も関係しているかもしれないので一応載せておきます。(まだ未解決なので、もしよろしければこちらも見ていただけると有り難いです。)
https://teratail.com/questions/u4zzcufouo9c9n
開発環境
・wsl2のubuntuを使っています
・ruby 3.0.3
・Rails 6.1.5
・Dockerで環境構築を行いました。
プログラミング入門者なので何かしら間違えた認識をしているかもしれません。
何かしらアドバイスがあればよろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー