多张图片上传错误

error with multiple image upload

我是新手 rails 4 我想添加多个 images.i 在我的产品模型和图片模型中 app.product 有很多图片并且图片有图像及其回形针附件 但我无法在我的视图中访问图像 models/products.rb

class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :pictures, :dependent => :destroy
end

models/picture.rb

class Picture < ActiveRecord::Base
belongs_to :product
accepts_nested_attributes_for :images,:allow_destroy => true
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/images/:id/:filename",
:url  => "/images/:id/:filename"

do_not_validate_attachment_file_type :image
end

查看页面

<div class="row">
                <% @products.each do |product| %>
                <div class="col-sm-3  col-lg-3 col-md-3">
                    <div class="thumbnail">

                        <%= product.image.url(:thumb) %>
                        <div class="caption">
                            <h4 class="pull-right">Rs.<%= product.price %>   </h4>
                            <h4><%= link_to 'Show', product %>
                            </h4>
                            <p><strong>Name:</strong>&emsp;  <%= product.name %></span></p>
                            <% if !product.description.blank? %>
                                <p><strong>Description:</strong> <%= product.description %></p>
                            <% end %>
                            <% if !product.reason.blank? %>
                                <p><strong>Reason:</strong><%= product.reason %></p>
                            <% end %>

                        </div>

                    </div>
                </div>
                <% end %>
</div>   

请试试这个:

<% for asset in @picture.assets %>
 <%= link_to image_tag(asset.image.url(:thumb)), asset.image.url(:original) %>
<% end %>

希望对您有所帮助。您也可以关注下方link:

https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb

您正试图在父模型上调用嵌套方法。

你有:

#app/models/product.rb
class Product < ActiveRecord::Base
   has_many :pictures
end

#app/models/picture.rb
class Picture < ActiveRecord::Base
   belongs_to :product
   has_attached_file :image
end

这意味着您必须致电:

<% @products.each do |product| %>
   <% product.pictures.each do |picture| %>
      <%= picture.image.url %>
   <% end %>
<% end %>

--

您收到 noMethod 错误,因为您试图在 Product 对象(不存在)上调用 image

<%= picture.image.url #-> doesn't exist %>

修复

修复比您意识到的要复杂一些。

具体来说,您必须确保能够 add/create 来自 accepts_nested_attributes_for 方法的嵌套 Picture 对象:

#app/models/product.rb
class Product < ActiveRecord::Base
   has_many :pictures
   accepts_nested_attributes_for :pictures 
end

这将允许您创建适当的表单和控制器操作:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def new
      @product = Product.new
      @product.pictures.build
   end

   def create
      @product = Product.new product_params
      @product.save
   end

   private

   def product_params
      params.require(:product).permit(..., pictures_attributes: [:image])
   end
end

然后您将使用 fields_for:

#app/views/products/new.html.erb
<%= form_for @product do |f| %>
   <%= f.fields_for :pictures do |p| %>
     <%= p.file_field :image %>
   <% end %>
   <%= f.submit %>
<% end %>

这将允许您使用新产品保存新的 Picture/images,然后使用上面的代码,您将能够展示它们!!


奖金

我一直分享的一个专业提示是 paperclip_defaults:

#config/application.rb
...
config.paperclip_defaults = {
   :styles => {
       :thumb    => ['100x100#',  :jpg, :quality => 70],
       :preview  => ['480x480#',  :jpg, :quality => 70],
       :large    => ['600>',      :jpg, :quality => 70],
       :retina   => ['1200>',     :jpg, :quality => 30] #-> is this correct formatting?
   },
   :path => ":rails_root/public/images/:id/:filename",
   :url  => "/images/:id/:filename"
}

您可以在 config/application.rb 中将它们设置为 "defaults",而不是在您的 Picture 模型中包含所有样式 - 这样您就可以调用 has_attached_file :image ,根据需要更改样式:

#app/models/picture.rb
class Picture < ActiveRecord::Base
   has_attached_file :image #-> will use the defaults which can be overridden as required
end