Jquery基本文件上传

时间:2012-10-25 00:37:43

标签: jquery ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我一直在研究由Ryan提出的一个名为Jquery文件上传的截屏视频。但是,每次输入图像时都不会上传。我不确定我的错误在于我的代码。我正在使用谷歌并尝试上传它但它似乎没有做任何事情

的Javascript 的application.js

//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require_tree .

painting.js.coffee

jQuery ->
  $('#new_painting').fileupload

浏览 _form.html.erb

<%= form_for @painting, :html => {:multipart => true} do |f| %>
  <div class="field">
    <%= f.hidden_field :galley_id %>
  </div>
  <div class="field">
    <%= f.label :image %><br />
    <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

index.html.erb

绘画廊

  
<%= form_for Painting.new do |f| %>
  <%= f.label :image, "Upload paintings:" %>
  <%= f.file_field :image, multiple: true, name: "painting[image]" %>
<% end %>

show.html.erb

<p><%= image_tag @painting.image_url %></p>

<p>
  <%= link_to "Edit", edit_painting_path(@painting) %> |
  <%= link_to "Destroy", @painting, :confirm => 'Are you sure?', :method => :delete %> |
  <%= link_to "Back to Paintings", paintings_path %>
</p>

控制器

def index
    @paintings = Painting.all
  end

  def show
    @painting = Painting.find(params[:id])
  end

  def new
    @painting = Painting.new
  end

  def create
    @painting = Painting.create(params[:painting])
  end

  def edit
    @painting = Painting.find(params[:id])
  end

  def update
    @painting = Painting.find(params[:id])
    if @painting.update_attributes(params[:painting])
      redirect_to paintings_url, notice: "Painting was successfully updated."
    else
      render :edit
    end
  end

  def destroy
    @painting = Painting.find(params[:id])
    @painting.destroy
    redirect_to paintings_url, notice: "Painting was successfully destroyed."
  end

模型

  attr_accessible :image
  mount_uploader :image, ImageUploader

我的主要问题是,当上传任何图片时,它没有提交按钮而不确定如何使其工作,我是否错过了什么?

2 个答案:

答案 0 :(得分:1)

所以只需通过在S3Uploader&gt;中添加时区(即utc)来解决这个问题。初始化方法

了解更多信息,请参阅: upload to amazon S3

答案 1 :(得分:0)

您可能已成功将文件上传到您的控制器操作,但除了

之外,您没有使用该文件。
@painting = Painting.create(params[:painting])

您需要做更多工作,例如将上传的文件保存在服务器的某个位置?

def  create
  filename = params[:painting][:image].original_filename
  filetype = params[:painting][:image].content_type
  filedata = params[:painting][:image].read
  File.open("#{Rails.root.to_s}/images/#{filename}","wb") { |f| f.write(filedata) } 
  @painting = Painting.create(params[:painting])
end

这将提取上传的图像并将其写入服务器。如果您想在数据库中记录MIME类型,Content_type也很方便。