根据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 数据。 我需要怎么修改才能实现不跳转,谢谢