前提・実現したいこと
ruby on rails でファイルファイルやユーザーを管理する機能を作っています。
発生している問題・エラーメッセージ
少し前までは下記のコードでファイルをうまくアップロードすることができていたのですが、他の機能を実装しているうちになぜかうまくdbに保存されなくなっていました。ファイルを選択してuploadボタンを押すと、エラー文などは出ないのですがDBにファイルが保存されていないまま、index.html.erbに移動してしまいます。new.html.erbとsubmits_controllerのnewアクション,createアクションは全くいじっておりません。
このような場合どのような点に着目して、修正すればいいのでしょうか。ご享受していただけると幸いです。
### 該当のソースコード __new.html.erb__ <div class="contents row"> <% if current_user.group.id == 0 %> <% else %> <%= form_for(@content, :url => {:controller => :submits, :action => :create}) do |f| %> <div class="field"> <%= f.file_field :upload_file %> </div> <div class="actions"> <%= f.submit "Upload" %> </div> <% end %> <% end %> </div>
__submits_controller__ class SubmitsController < ApplicationController def index end def new @content = Content.new end def create upload_file = content_params[:upload_file] content = {} if upload_file != nil content[:upload_file] = upload_file.read content[:upload_file_name] = upload_file.original_filename end @content = Content.new(content) respond_to do |format| if @content.save format.html { redirect_to ("/submits/create"), notice: 'Upload success' } format.json { render action: 'show', status: :created, location: @content } else @contents = Content.all format.html { render action: 'index' } format.json { render json: @content.errors, status: :unprocessable_entity } end end end private def content_params params.require(:content).permit(:upload_file) end end ``` ### 試したこと binding.pryで確認したところ、うまくパラメーターにupload_fileがおくられていませんでした ### 補足情報(FW/ツールのバージョンなど) Rails 5.2.2
あなたの回答
tips
プレビュー