我有一個項目模型,我可以創建項目和每個項目上傳視頻和照片。我使用嵌套屬性。我的表單顯示5個照片上傳按鈕和5個視頻上傳按鈕。我想隱藏使用的按鈕。例如:如果我上傳了1張照片和2個視頻,我希望我的編輯表單顯示4個上傳照片按鈕和3個視頻。顯示上傳按鈕取決於上傳的文件
現在,如果上傳照片,它將隱藏1個上傳按鈕和1個來自視頻的上傳按鈕。有誰知道如何使這成爲可能?
這是我的控制器。
def new
@project = Project.new()
(5 - @project.assets.length).times { @project.assets.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find_by_permalink(params[:id])
(5 - @project.assets.length).times { @project.assets.build }
end
寶石:
gem "paperclip", "~> 3.5"
# handels video upload
gem "paperclip-ffmpeg", "~> 1.0.0"
項目型號:
class Project < ActiveRecord::Base
has_permalink :title
default_scope :order => 'created_at desc'
attr_accessible :title, :description, :assets_attributes, :dependent => :destroy
validates_uniqueness_of :title
validates_presence_of :title
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
end
資產型號:
class Asset < ActiveRecord::Base
require 'paperclip'
require 'paperclip-ffmpeg'
belongs_to :project, :foreign_key => "project_id"
attr_accessible :project_id, :photo, :video
has_attached_file :photo, :styles => { :thumb => "130x130#", :medium => "300x300#", :small => "160x160#"}
has_attached_file :video, :styles => {
:mobile => {:geometry => "400x300", :format => 'flv', :streaming => true}
}, :processors => [:ffmpeg, :qtfaststart]
end
增加了一些信息:) – fynn