[英]Rails 3 Ajax file upload unsuccessful with remotipart gem
I am having trouble with Ajax file uploading in Rails 3.0.5 Ruby v 1.8.7 using the gem 'remotipart', '~> 0.4'. 我在使用gem'remotipart','〜> 0.4'在Rails 3.0.5 Ruby v 1.8.7中上载Ajax文件时遇到麻烦。 I have success with the alerts showing up when I remove the if @asset.save? 当删除if @ asset.save吗? statement in the controller but when its there the js file isn't called. 控制器中的语句,但是在那里没有调用js文件。 For some reason the assets are not being saved. 由于某种原因,资产没有被保存。
Any ideas? 有任何想法吗?
Controller: 控制器:
def create
@asset = Asset.new(params[:asset])
respond_to do |format|
if @asset.save
format.html
format.js
end
end
end
View: 视图:
<%= form_for @asset, :remote => true, :html => {:multipart => true}, :id => "new_asset_form" do |f| %>
<%= render :partial => 'shared/error_messages', :locals => { :target => @asset } %>
<%= f.hidden_field :company_id, :value => current_company.id %>
<%= f.file_field :asset %>
<%= submit_tag 'Add', :class => 'submit' %>
<% end %>
Create.js.erb: Create.js.erb:
<%= remotipart_response do %>
// Display a Javascript alert
alert('success!');
<% if remotipart_submitted? %>
alert('submitted via remotipart')
<% else %>
alert('submitted via native jquery-ujs')
<% end %>
<% end %>
Terminal Log: 终端日志:
Started POST "/assets" for 127.0.0.1 at Thu May 10 14:45:40 -0400 2012
Processing by AssetsController#create as JS
Parameters: {"commit"=>"Add", "authenticity_token"=>"9TDxFQfTGrdz8gKti413FoIr1JUSwLGQQXv/tJQd+sY=", "utf8"=>"✓", "_"=>"", "asset"=>{"company_id"=>"5"}}
Company Load (0.5ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`subdomain` = 'demo' LIMIT 1
User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 295 AND (company_id = 5) LIMIT 1
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 295 AND (company_id = 5) LIMIT 1
Rendered assets/create.js.erb (0.7ms)
Completed 200 OK in 182ms (Views: 8.9ms | ActiveRecord: 1.2ms)
Thanks for any help. 谢谢你的帮助。 Been stuck for hours. 被困了几个小时。
尝试升级到Remotipart的最新版本,位于: http ://os.alfajango.com/remotipart/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.