我开始研究rails应用程序 我想在我的模型中添加多个图像。 的模型/ product.rb
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :product_images, :dependent => :destroy
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }
end
模型/ product_image.rb
class ProductImage < ActiveRecord::Base
belongs_to:product
has_attached_file :image ,:styles => {
:thumb => ['100x100#', :jpg, :quality => 70],
:preview => ['480x480#', :jpg, :quality => 70],
:large => ['600>', :jpg, :quality => 70],
:retina => ['1200>', :jpg, :quality => 30]},
:path => ':rails_root/public/system/:id.:extension',
:convert_options => {
:thumb => '-set colorspace sRGB -strip',
:preview => '-set colorspace sRGB -strip',
:large => '-set colorspace sRGB -strip',
:retina => '-set colorspace sRGB -strip -sharpen 0x0.5'
}
validates_attachment_presence :image
validates_attachment_size :image , :less_than => 10.megabytes
validates_attachment_content_type :image , :content_type =>['image/jpeg','image/jpg','image/png']
validates :image, presence: true
end
我的产品表格如下 的 _form.html.erb
<%= form_for @product, url: products_path, :html => {:multipart => true,:class => " form-horizontal center"} do |f| %>
<div class="field">
<%= f.label :name,class:"control-label" %>
<%= f.text_field :name,class:"form-control" %>
</div>
<div class="field">
<%= f.label :price,class:"control-label" %>
<%= f.number_field :price,class:"form-control"%>
</div>
<div class="field">
<%= f.label :description,class:"control-label" %>
<%= f.text_area :description,class:"form-control" %>
</div>
<div class="field">
<%= f.label :reason,class:"control-label" %>
<%= f.text_area :reason,class:"form-control" %>
</div><br/>
<div>
<%= f.label :status,class:"control-label col-md-1" %>
<div class="col-md-4">
<%= f.select :status, [["available"], ["sold"]], {}, {class: "form-control"} %>
</div>
<%= f.label :category_id,class:"control-label col-md-1" %>
<div class="col-md-4">
<%= f.collection_select :category_id, Category.all, :id, :name,{ :prompt => "Category", :multiple => true},{class: "form-control"} %>
</div>
<% fields_for :product_images do |builder| %>
<h1>
<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>
</h1>
<p>
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</p>
<% end %>
</span>
</div>
<br/>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div><br/>
<% end %>
&#13;
在我的表单中,我可以看到除了fileds_for标签中提到的标签之外的所有内容,用于多个图片上传。 有人可以帮我这个 我的问题已经解决了,但是你可以帮助我如何使用它来进行多个图像上传。因为我已经为图像循环我有更多的标签形式,我不想要那个。我想要一个图像选择输入,我需要上传多个图像。帮助我保存产品后显示图像
答案 0 :(得分:0)
由于fields_for
方法产生输出,您需要开头erb标记的等号形式:
<%= fields_for :product_images do |builder| %>
这个答案有很好的报道: What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
由于您使用的是accepts_nested_attributes_for
,因此您可能还希望在调用fields_for
时使用父表单构建器,除非您手动处理product_images值。
<%= f.fields_for :product_images do |builder| %>
有关Erubis
的更多信息,Rails使用的erb的风格:
http://www.kuwata-lab.com/erubis
答案 1 :(得分:0)
你必须写
<%= f.fields_for :product_images do |builder| %>