0

我开始开发一个 Rails 应用程序,我想将多个图像添加到我的模型中。 型号/产品.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 %>

在我的表格中,我可以看到除了用于多张图片上传的fileds_for标签中提到的标签之外的所有内容。有人可以帮我解决我的问题,但你能帮我如何使用它来上传多个图像。因为我有图像循环,我有更多的标签,我不想要那个。我想要一个图像选择输入,我需要通过那里上传多个图像。一旦产品保存,帮助我显示图像

4

2 回答 2

0

由于该fields_for方法产生输出,因此您需要开始 erb 标记的等号形式:

<%= fields_for :product_images do |builder| %>

这个答案很好地涵盖了这些变化: Rails 中的 ERB 中的 <%、<%=、<%# 和 -%> 有什么区别?

由于您正在使用accepts_nested_attributes_for,因此您可能还希望在调用时使用父表单构建器fields_for,除非您手动处理 product_images 值。

<%= f.fields_for :product_images do |builder| %>

有关ErubisRails 使用的 erb 风格的更多信息: http ://www.kuwata-lab.com/erubis

于 2015-12-23T21:42:29.230 回答
0

你必须写

<%= f.fields_for :product_images do |builder| %>
于 2015-12-23T22:04:52.737 回答