的辦法,我使用:
我有屬性,有許多照片(10時)。要WHE具有的代碼:
在屬性控制器:鑑於
def new
@search = Property.search(params[:search])
@property = Property.new
10.times { @property.photos.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @property }
end
end
# GET /properties/1/edit
def edit
@search = Property.search(params[:search])
@property = Property.find(params[:id])
# Se o usuário atual for dono da propriedade
if current_user.id == @property.user_id
@photos = Photo.where("property_id = ?", @property.id)
@N = @photos.count
@N = [email protected]
@N.times { @property.photos.build }
else
render :action => "show"
end
end
10.times 「渲染」 10倍的照片字段。 在編輯窗體時,只是照片照片領域。 例如: 我第一次上傳3張照片,如果我想上傳更多,只會出現7個字段。
在屬性模型,我有:
class Property < ActiveRecord::Base
attr_accessible :photos_attributes, :logradouro, :complemento, :wc, :negocio, :cep, :vagas, :valor,
:quartos, :uf, :area, :bairro, :suites, :salas, :numero, :cidade, :descricao,
:status, :tipoImovel
has_many :photos
accepts_nested_attributes_for :photos, :allow_destroy => true
end
這讓張照片上傳。
照片模式:
class Photo < ActiveRecord::Base
belongs_to :property
has_attached_file :photo, :styles => { :small => "100x100>", :medium => "530>x530", :large => "800x800>" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 500.kilobytes
end
在我的形式分:
<div id="new_up">
<%= f.fields_for :photos do |p| %>
<% if p.object.new_record? %>
<p><%= p.file_field :photo %>
<%= p.radio_button :miniatura, true -%>
</p>
<% end %>
<% end %>
</div>
<div id="old_up">
<h4>Imagens Atuais</h4>
<% f.fields_for :photos do |p| %>
<% unless p.object.new_record? %>
<div style="float: left;">
<%= p.radio_button :miniatura, true -%>
<%= link_to image_tag(p.object.photo.url(:small)), p.object.photo.url(:original) %>
<%= p.check_box :_destroy %>
</div>
<% end %>
<% end %>
</div>
我在尋找同樣的事情,你有沒有發現什麼呢?謝謝。 – 2011-02-04 22:10:27