2012-10-25 124 views
2

我一直在研究由Ryan提出的名爲Jquery文件上傳的screencast pro。不過每次我輸入圖片都不會上傳。我不知道我在這裏做錯了我的代碼。我使用谷歌並沒有嘗試上載它,但它似乎並沒有被做任何事情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 

我的主要問題是,當涉及到上傳任何圖片,它只是沒有一個提交按鈕,不知道如何使它工作,我錯過了什麼?

回答

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也很方便。

1

所以纔得到這個固定的,加入時區(即UTC)在S3Uploader到期>初始化方法

更多信息,請參閱: upload to amazon S3