jquery文件上传rails“;错误:文件上载结果为空";
我有这样的看法(形式部分): 在db中,我看到它被保存,但为什么在视图中我得到:jquery文件上传rails“;错误:文件上载结果为空";,jquery,ruby-on-rails,ruby,jquery-file-upload,Jquery,Ruby On Rails,Ruby,Jquery File Upload,我有这样的看法(形式部分): 在db中,我看到它被保存,但为什么在视图中我得到: Error Empty file upload result 我做错了什么?如何解决呢 jquery文件上载库需要特定格式的JSON响应: 这可能就是问题所在 顺便说一下,请不要像您那样发送响应,我的意思是,不要在控制器中调用视图方法。只发送url作为响应,并在fileUpload回调中添加标记,或者在视图中呈现它(例如使用jbuildergem)“顺便说一下,请不要像您那样发送响应,我的意思是,不要在控制器
Error Empty file upload result
我做错了什么?如何解决呢 jquery文件上载库需要特定格式的JSON响应: 这可能就是问题所在
顺便说一下,请不要像您那样发送响应,我的意思是,不要在控制器中调用视图方法。只发送url作为响应,并在fileUpload回调中添加
标记,或者在视图中呈现它(例如使用jbuilder
gem)“顺便说一下,请不要像您那样发送响应,我的意思是,不要在控制器中调用视图方法。要么发送url作为响应并在fileUpload回调中添加标记,要么在视图中呈现它(使用例如jbuilder gem)“不理解您,可能会给出一些代码)确定。在您的控制器中,您有如下内容:format.json{data={id:@car\u photo.id,thumb:view\u context.image\u标记(@car\u photo.car\u photo.url(:thumb))
这是一种不好的做法。image\u-tag
方法属于视图,不应在控制器中使用,您在这里混合了职责。因此,可能的解决方案是执行以下操作之一:format.json{data={thumb:@car\u-photo.car\u-photo.url(:thumb)}
然后在前端用Javascript处理它,或者只使用format.json
,并将汽车照片/create.json.jbuilder
这样的文件放在视图信息的位置。实际上,将这两个文件结合起来是最好的选择:使用view file并将url放在那里,而不是
标记,然后在Javascript.Le中添加
我不知道它是否仍然不清晰,这与你使用的宝石有关。如果你使用jbuilder
它看起来可能是这样的:是的,它工作正常,回答很好。ps:uwielbiam Polskę:)
class CarPhotosController < ApplicationController
before_action :set_car_photo, only: [:show, :edit, :update, :destroy]
def new
@car_photo = CarPhoto.new
end
def create
#binding.pry
@car_photo = CarPhoto.new(car_photo: car_photo_params[:car_photo].first)
#binding.pry
#@car_photo.user_id = current_user.id
respond_to do |format|
if @car_photo.save #&& user_signed_in?
format.html { redirect_to @car_photo, notice: 'Photo was successfully created.' }
format.json { data = {id: @car_photo.id, thumb: view_context.image_tag(@car_photo.car_photo.url(:thumb))}
render json: data, status: :created, location: @car_photo }
else
format.html { render action: "new" }
format.json { render json: @car_photo.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_car_photo
@car_photo = CarPhoto.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def car_photo_params
params.require(:car_photo).permit(:car_photo, {:car_photo => []})
end
end
{"id":31,"thumb":"\u003Cimg alt=\"1\" src=\"/system/car_photos/car_photos/000/000/031/thumb/1.jpg?1394785415\" /\u003E"}
Error Empty file upload result