使用jQuery上传获取错误
ActionController::ParameterMissing - param is missing or the value is empty: project:
我添加了一个新的add_photos.html.erb
页面,因为我想在有人在表单中提交项目后添加照片,这就是为什么在下面的控制器中,创建后,它会重定向以添加照片。
这是我的add_photos.html.erb
<%= simple_form_for @project, html: { multipart: true, id: 'fileupload' } do |f| %>
<input type="file" name="photos[]" id='photo_upload_btn', multiple>
<% end %>
<script>
$(function () {
'use strict';
$('#fileupload').fileupload({
});
$('#fileupload').addClass('fileupload-processing');
$.ajax({
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
context: $('#fileupload')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {result: result});
});
});
</script>
我的控制器
def add_photos
@project = current_user.projects.find(params[:id])
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
if params[:photos]
params[:photos].each { |image|
@project.project_images.create(photo: image)
}
end
format.html { redirect_to add_photos_project_path(@project), notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
我的路线
resources :projects do
member do
get 'add-photos'
post 'upload_photos'
end
end
答案 0 :(得分:0)
尝试添加
processData: false,
contentType: false,
到ajax请求。我遇到了同样的问题而且弄得一团糟。
$.ajax({
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
processData: false,
contentType: false,
context: $('#fileupload')[0]
}).always(function() {
$(this).removeClass('fileupload-processing');
}).done(function(result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {
result: result
});
});
并且只是说:对于你的表格,params [:project]总是空的。它只是一个空的形式。一般来说,我会建议你去nested_form。告诉你的项目它接受nested_attributes_for:project_images然后它的params [:project] [:photos]