2

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

Javascript 应用程序.js

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

绘画.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 %>

显示.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

我的主要问题是上传任何图片时它没有提交按钮并且不知道如何使它工作,我错过了什么吗?

4

2 回答 2

1

因此,只需通过在 S3Uploader > 初始化方法中将时区(即 utc)添加到过期时间来解决此问题

有关更多信息,请参阅: 上传到亚马逊 S3

于 2013-01-19T21:30:40.353 回答
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 很方便。

于 2012-11-05T19:15:13.040 回答