多个图片上传时出错

时间:2015-12-25 05:19:02

标签: ruby-on-rails paperclip

Iam new to rails 4我想要添加多个images.i我的app.product有一个产品型号和图片模型有很多图片和图片有一个图像和它的回形针附件 但我无法访问我的视图中的图像 的模型/ products.rb

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

模型/ 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>   

2 个答案:

答案 0 :(得分:1)

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

你有:

#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错误,因为您试图在image对象(不存在)上调用Product

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

<强>修正

修复比你意识到的要多一些。

具体来说,您必须确保能够添加/创建嵌套Picture对象,这些对象来自accepts_nested_attributes_for方法:

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

这将允许您创建适当的表格&amp;控制器动作:

#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"
}

您可以在Picture中将其设置为“默认值”,而不是在config/application.rb模型中包含所有样式 - 这样您就可以调用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

答案 1 :(得分:0)

请试试这个:

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

我希望它会对你有所帮助。您还可以点击以下链接:

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