新手问题 请教 jQuery-File-Upload 实现无刷新页面上传图片

zqalyc · 2013年05月09日 · 最后由 zqalyc 回复于 2013年05月09日 · 6212 次阅读

根据https://github.com/tors/jquery-fileupload-rails安装了 jQuery-File-Upload 插件 步骤: 1.gem "jquery-fileupload-rails" 2.//= require jquery-fileupload 3.*= require jquery.fileupload-ui

然后写了 controller 方法: def create if @photo=Photo.find_by_user_id(current_user)

@photo.original_name=params[:photo]["image"].original_filename @user=current_user if @photo.update_attributes(params[:photo]) @photo.remote_photo_path=current_user.photo.image.instance_values['storage'].uploader.to_s.insert(-37,"thumb_") if @photo.save respond_to do |format| format.html { render :html => [@photo.to_jq_upload].to_json, :content_type => 'text/html', :layout => false } format.json { render :json => [@photo.to_jq_upload].to_json } end

form 表单: <%= form_for @photo,:url => { :action => "create", :controller=>"photos" }, :html => {:multipart => true,:id=>"fileupload"} do |f| %> <%= f.hidden_field :user_id %> <%= f.file_field :image ,:class => "field_style" %> <%=f.submit "submit"%> <% end %>

$(function () { $('#fileupload').fileupload({ event.preventDefault(); $.ajax({ url: '/photos', type: 'POST', data: {id : <%= <a href="/photo.id" class="user-mention" title="@photo.id"><i>@</i>photo.id</a> %>} }); });

一点击提交就会跳转到 controller,页面显示的是返回的 json 数据。 我需要怎么修改才能实现不跳转,谢谢

respond_to :html, :json

#1 楼 @SharpX 我改成上面的那个样子了,render :html =>和 render :json => ,我发现 form 表单是错的能帮我看下怎么写么

需要 登录 后方可回复, 如果你还没有账号请 注册新账号