直接貼Code好不好? 噗!

Controller:

def uploading
    files = []
    for i in 1..session[:files].to_i
      next if  params[:gallery]["file_#{i}".to_sym].to_s.blank?
      files << Gallery.create(:file => params[:gallery]["file_#{i}".to_sym]).id.to_s
    end
    redirect_to :action => "images", :files => files.join("_")
  end
 
  def new_upload_field
    session[:files]+=1
    render :update do |page|
      page.insert_html :after, "file_fields", file_column_field("gallery", "file_#{session[:files]}") + "<br />"
    end
  end

 

View:

<%= javascript_include_tag :defaults %>
<% form_for(Gallery.new, :url => {:action => "uploading"}, :html => {:multipart => true}) do |f| -%>
    <div id="file_fields">
        <%= file_column_field("gallery", "file_#{session[:files]}")%><%= link_to_remote("Add new", :url => {:action => "new_upload_field"}) %>
    </div>
  <%= f.submit("Upload") %>
<% end %>

 

解說:

用RJS產生上傳欄位,然後每次新增一個都更新一次檔案數目

接著跑迴圈儲存物件就好了

arrow
arrow
    全站熱搜

    hechian 發表在 痞客邦 留言(2) 人氣()