Ruby on rails 标签的字段_未显示任何内容rails 4

Ruby on rails 标签的字段_未显示任何内容rails 4,ruby-on-rails,rails-4-2-1,Ruby On Rails,Rails 4 2 1,我开始开发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: { medi

我开始开发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
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
类产品:销毁
已附加文件:图像,样式:{medium:“300x300>”,thumb:“100x100>”},默认url:“/images/:style/missing.png”
接受:product_图像的_嵌套的_属性_,:reject_if=>lambda{t | t['product_图像'].nil?}
结束
型号/产品图片.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
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
class-ProductImage{
:thumb=>['100x100#',:jpg,:quality=>70],
:preview=>['480x480#',:jpg,:quality=>70],
:large=>['600>,:jpg,:quality=>70],
:retina=>['1200>,:jpg,:quality=>30]},
:path=>':rails\u root/public/system/:id:扩展名',
:convert_options=>{
:thumb=>'-set colorspace sRGB-strip',
:preview=>'-设置颜色空间sRGB-条带',
:large=>'-set colorspace sRGB-strip',
:retina=>'-设置颜色空间sRGB-条带-锐化0x0.5'
}
验证\u附件\u是否存在:映像
验证\u附件\u大小:图像,:小于=>10.MB
验证附件内容类型:图像、内容类型=>['image/jpeg'、'image/jpg'、'image/png']
验证:映像,状态:true
结束
我的产品表如下 \u form.html.erb

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
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
{:multipart=>true,:class=>“形成水平中心”}do | f |%>

“类别”,:multiple=>true},{class:“表单控件”}%>




由于方法的
字段\u产生输出,因此需要开头erb标记的等号形式:

这个答案很好地涵盖了各种变化:

由于您正在为
使用
接受\u嵌套的\u属性,\u,因此在为
调用
字段\u时,您可能还希望使用父表单生成器,除非您手动处理产品图像值

有关
Erubis
的更多信息,请参阅Rails使用的erb风格: 你必须写作

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


谢谢您的帮助。如果一次最多上传3张图片,我想使用此表单进行多张图片上传。我实现了3.times{@product.product\u images.build}在我的product.new方法中,由于它是一个循环,它在表单中显示选择图像文件3次。你能帮我一下吗?我在显示产品图像方面也有问题。关于堆栈溢出的每个问题只涉及一个主题。试着把你的问题降到最小的可重复的情况。然后在一个新问题中发布你的问题(和代码!)。欢迎来到堆栈溢出!几个请求……您能编辑问题标题,将
字段更改为
字段吗?要找到现有的答案可能很困难,但请在堆栈溢出搜索框中尝试几种组合。我在show
(标题中的三个词)的
rails字段(u
)下找到了一些答案,但在display的
rails字段(u)下找到了更好的答案。即使你没有找到这样的答案,你也会经常看到一些你还不知道的问题的线索。