ruby - Image uploads in Rails with jQuery plugins -
i have create prouct page , add photos page. after user creates product redirected page allows them add photos. i'm using jquery file-upload shows preview of pictures selected , upon clicking start adds them server. however, error.
jquery file-upload error
dwadecrop.jpg 97.74 kb error syntaxerror: json.parse: unexpected character
console message
started post "/photos" 127.0.0.1 @ 2013-07-10 21:03:03 -0400 processing photoscontroller#create json parameters: {"utf8"=>"✓", "authenticity_token"=>"tc6sbe9n/ppklzlupqywqxr3c6qjemnqjskr8+prczw=", "product_id"=>"129", "photo"=>{"upload"=>#<actiondispatch::http::uploadedfile:0xa5f0780 @original_filename="dwadecrop.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"photo[upload]\"; filename=\"dwadecrop.jpg\"\r\ncontent-type: image/jpeg\r\n", @tempfile=#<file:/tmp/rackmultipart20130710-3967-h6rcoo>>}} product load (0.1ms) select "products".* "products" "products"."id" = ? limit 1 [["id", "129"]] redirected http://localhost:3000/ completed 302 found in 5ms (activerecord: 0.1ms) started "/" 127.0.0.1 @ 2013-07-10 21:03:03 -0400 processing staticpagescontroller#home json rendered static_pages/home.html.haml within layouts/application (1.4ms) user load (0.3ms) select "users".* "users" "users"."auth_token" = 'eqoqkrrsnzfa51iidq-90w' limit 1 completed 200 ok in 85ms (views: 84.6ms | activerecord: 0.3ms)
upload photo view location localhost:3000/products/123/pics
upload file = form_for @photo, :html => { :multipart => true, :id => "fileupload" } |f| = f.file_field :upload = hidden_field_tag 'product_id', @photo.product_id
method pics
def pics @product = product.find(params[:product_id]) @photo = photo.new @photo.product_id = @product.id end
create method photos
def create @product = product.find(params[:product_id]) @photo = photo.new if @photo.valid? @photo.product_id = @product.id @photo.save! respond_to |format| format.html { redirect_to product_path(@product) } format.json { render json: @photo } end else redirect_to root_url, :notice => "somehting went wrong!" end end
photo model
class photo < activerecord::base attr_accessible :image has_attached_file :image include rails.application.routes.url_helpers belongs_to :product validates_attachment :image, presence: true, content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, size: { less_than: 5.megabytes } has_attached_file :image, styles: { medium: "320x240>", :thumb => "100x100>"} def to_jq_image { "name" => read_attribute(:upload_file_name), "size" => read_attribute(:upload_file_size), "url" => image.url(:original), "delete_type" => "delete" } end end
the problem in photo model have
"name" => read_attribute(:upload_file_name), "size" => read_attribute(:upload_file_size),
when model @photo change
"name" => read_attribute(:photo_file_name), "size" => read_attribute(:photo_file_size),
also in form_for have
upload file = form_for @photo, :html => { :multipart => true, :id => "fileupload" } |f| = f.file_field :upload
where says :upload again model @photo replace well.
Comments
Post a Comment