我正在开发一个包含文件上传页面的Rails项目。我正在使用CarrierGem。我收到以下错误:
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"}
以下是我的文件:
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
端
查看: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' %>
查看:_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 %>
型号: class PatternSubmit&lt;的ActiveRecord ::基
has_one:user validates_presence_of:title mount_uploader:attachment,PatternUploader validates_presence_of:附件
端
答案 0 :(得分:0)
:attachment
不是params
的关键字;它是params[:pattern_submit]
的关键。
尝试
def pattern_submit_params
params.require(:pattern_submit).require(:attachment)
end
或只是
def pattern_submit_params
params.require(:pattern_submit)
end
取决于PatternSubmit.new
的要求。