field_for 标签未显示任何内容 rails 4
field_for tag not showing anything rails 4
我开始开发 rails 应用程序
我想向我的模型添加多个图像。
model/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
model/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 标签中提到的用于多张图片上传的标签之外的所有内容。
有人可以帮我吗
我的问题已经解决了,但是你能帮我如何使用它来获得多个图像 upload.since 我正在为图像使用 for 循环 我在表格中有更多标签,我不想 that.i 想要一张图片选择输入,我需要通过 there.help 上传多张图片,同时在产品保存后显示图片
由于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
你必须写
<%= f.fields_for :product_images do |builder| %>
我开始开发 rails 应用程序 我想向我的模型添加多个图像。 model/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
model/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 标签中提到的用于多张图片上传的标签之外的所有内容。 有人可以帮我吗 我的问题已经解决了,但是你能帮我如何使用它来获得多个图像 upload.since 我正在为图像使用 for 循环 我在表格中有更多标签,我不想 that.i 想要一张图片选择输入,我需要通过 there.help 上传多张图片,同时在产品保存后显示图片
由于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
你必须写
<%= f.fields_for :product_images do |builder| %>