是否使用Ajax在Ruby on Rails的CarrierWave宝石的工作?工作、Ruby、Ajax、on

2023-09-10 16:22:18 作者:萨拉嘿呦

由于某些原因使用CarrierWave宝石与阿贾克斯似乎并不为我工作。难道我做错了什么?我跟着253 CarrierWave Railscast以及和它的作品没有Ajax,但在我的应用程序需要使用AJAX。这是我的code:

For some reason using the CarrierWave gem with Ajax doesn't seem to be working for me. Am I doing something wrong? I followed the 253 CarrierWave Railscast well and it works without AJAX but in my application I need to use AJAX. Here is my code:

在映像文件字段中选择一个JPEG后PARAMS清单:

The params list after selecting a jpeg in the image file field:

Parameters: {"item"=>{"remote_image_url"=>""}}

new.html.erb:

new.html.erb:

<%= form_for(@item, :url => create_item_path, :html => {:id => "create_item_form", :multipart => true}) do |f| %>
    <p>
      <%= f.file_field :image %>
    </p>
    <p>
      <%= f.label :remote_image_url, "or image URL" %><br />
      <%= f.text_field :remote_image_url %>
    </p>
    <%= f.submit "Save", :id => "save_button" %>
<% end %>

的application.js

application.js

$("#create_item_form").submit(function() {
    $.ajax({
      type: "POST",
      url: $(this).attr("action"),
      dataType: "script",
      data:  $("#destination_item").sortable('serialize') + "&" + $(this).serialize()
      });
      return false;
});

item.rb的

item.rb

class Item < ActiveRecord::Base
  attr_accessible :description, :image, :remote_image_url
  belongs_to :user
  has_many :item_sub
  mount_uploader :image, ImageUploader
end

schema.rb

schema.rb

  create_table "item", :force => true do |t|
    t.integer  "user_id"
    t.string   "title"
    t.string   "image"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

我在我的Gemfile的carrierwave宝石,我还没有在app /上传者/ image_uploader.rb改变任何东西。

I have the carrierwave gem in my gemfile and I haven't changed anything in the app/uploaders/image_uploader.rb.

感谢您的帮助!

推荐答案

没有什么可以没有使用像Uploadify库来完成。这是因为XMLHtt prequest(AJAX)的标准有文件上传的支持。你可以真的假的,这是使用与Flash一个iFrame的唯一途径。 Uploadify是最好的其中一个选项,它具有最佳的文档。这就是,必须在客户端(浏览器)完成的。 Uploadify真的不是一个红宝石的宝石,闪光和JS的一个集合,让浏览器假的。

There is nothing that can be done without a using a library like Uploadify. This is because the XMLHttpRequest (AJAX) standard has no support for file uploads. The only way you can really fake this is using an iFrame with Flash. Uploadify is the best of these options, and it has the best documentation. This is what has to be done on the client side (browser). Uploadify really isn't a ruby gem, its a collection of flash and js to allow the browser to 'fake it'.

在服务器端,您可以使用carrierwave支持上传,但你需要一种方法来从客户端让他们那里。这是一个非常类似的问题应该给你,你需要的说明。

On the server side, you can use carrierwave to support the uploads, but you need a way to get them there from the client side. Here is an extremely similar question which should give you the instructions that you need.

Rails的载波与jQuery上传

希望这有助于