0
votes

I am working on a Rails project that has a page for file upload. I am using the CarrierGem. I am getting the following error:

ActionController::ParameterMissing in PatternSubmitsController#create
param is missing or the value is empty: attachment

Extracted source (around line #18):
16
17
18
19
20
21

private 
  def pattern_submit_params
    params.require(:attachment)
  end

end

Rails.root: /home/action/workspace/upcraftproject

Application Trace | Framework Trace | Full Trace
app/controllers/pattern_submits_controller.rb:18:in `pattern_submit_params'
app/controllers/pattern_submits_controller.rb:8:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"8khnKtWJjzTj6grHX50W7OzO148j8JtnQjTWYs6O4pM=",
 "pattern_submit"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x00000002c7e4d8 @tempfile=#<Tempfile:/tmp/RackMultipart20141208-506-1cygw8i>,
 @original_filename="photo 2.JPG",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"pattern_submit[attachment]\"; filename=\"photo 2.JPG\"\r\nContent-Type: image/jpeg\r\n">},
 "commit"=>"Upload"}

Here are my files:

pattern_submits_controller.rb:
class PatternSubmitsController < ApplicationController

def new
  @pattern_submit = PatternSubmit.new
end

def create
  @pattern_submit = PatternSubmit.new(pattern_submit_params)
  if @pattern_submit.save
    redirect_to @pattern_submit
  else
    render "new"
  end
end

private 
  def pattern_submit_params
    params.require(:attachment)
  end

end

View: new.html.erb

<h1>Submit a pattern submission</h1>
<% if @pattern_submit.errors.any? %>
<div class="error_messages">
<h2>You're missing an attachment</h2>
<ul>
  <% @pattern_submit.errors.full_messages.each do |message| %>
  <li><%= message %></li>
  <% end %>
</ul>
</div>
<% end %>
<%= render :partial => 'form' %>

View: _form.html

<%= form_for PatternSubmit.new, :html => {:multipart => true} do |f| %>
<p>
<label>Pattern Upload</label>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
</p>
<p>
<div class="form actions">
<%= f.submit "Upload" %>
</div>
</p>
<% end %>

Model: class PatternSubmit < ActiveRecord::Base

has_one :user validates_presence_of :title mount_uploader :attachment, PatternUploader validates_presence_of :attachment

end

1

1 Answers

0
votes

:attachment is not a key of params; it is a key of params[:pattern_submit].

Try

def pattern_submit_params
  params.require(:pattern_submit).require(:attachment)
end

or just

def pattern_submit_params
  params.require(:pattern_submit)
end

depending on what is required by PatternSubmit.new.