多张图片上传错误 [英] error with multiple image upload

查看:46
本文介绍了多张图片上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 4 的新手,我想添加多个图像.我的应用中有一个产品模型和图片模型.产品有很多图片,图片有一个图像及其回形针附件但我无法访问我认为的图像models/products.rb

class Product :破坏结尾

models/picture.rb

类图片真的has_attached_file :image,:styles =>{:thumb =>['100x100#', :jpg, :quality =>70],:预览 =>['480x480#', :jpg, :quality =>70],:大 =>['600>', :jpg, :quality =>70],:视网膜 =>['1200>', :jpg, :quality =>30]},:路径=>":rails_root/public/images/:id/:filename",:url =>"/images/:id/:filename"do_not_validate_attachment_file_type :image结尾

查看页面

<% @products.each 做 |product|%><div class="col-sm-3 col-lg-3 col-md-3"><div class="缩略图"><%= product.image.url(:thumb) %><div class="caption"><h4 class="pull-right">Rs.<%= product.price %><h4><%= link_to 'Show', product %><p><strong>名称:</strong>&emsp;<%= product.name %></span></p><% 如果 !product.description.blank?%><p><strong>说明:</strong><%=产品.描述%</p><%结束%><% 如果 !product.reason.blank?%><p><strong>原因:</strong><%= product.reason %></p><%结束%>

<%结束%>

解决方案

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

你有:

#app/models/product.rb类产品

这意味着您必须调用:

<% @products.each do |product|%><% product.pictures.each do |picture|%><%=图片.image.url %><%结束%><%结束%>

--

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

<%= picture.image.url #->不存在 %>

<小时>

修复

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

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

#app/models/product.rb类产品

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

#app/controllers/products_controller.rb类 ProductsController <应用控制器定义新@product = Product.new@product.pictures.build结尾定义创建@product = Product.new product_params@product.save结尾私人的def product_paramsparams.require(:product).permit(...,pictures_attributes: [:image])结尾结尾

然后您将使用 fields_for:

#app/views/products/new.html.erb<%= form_for @product do |f|%><%= f.fields_for :图片做 |p|%><%= p.file_field :image %><%结束%><%= f.submit %><%结束%>

这将允许您使用新产品保存新的图片/图像,然后使用上面的代码,您将能够显示它们!!

<小时>

奖金

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

#config/application.rb...config.paperclip_defaults = {:styles =>{:thumb =>['100x100#', :jpg, :quality =>70],:预览 =>['480x480#', :jpg, :quality =>70],:大 =>['600>', :jpg, :quality =>70],:视网膜 =>['1200>', :jpg, :quality =>30] #->这是正确的格式吗?},:路径=>":rails_root/public/images/:id/:filename",:url =>"/images/:id/:filename"}

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

#app/models/picture.rb班级图片将使用可以根据需要覆盖的默认值结尾

Iam new to rails 4 i want add multiple images.i have a product model and picture model in my app.product has many picture and picture has a image and its a paperclip attachment but i cant access images in my view 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

View page

<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>   

解决方案

You're trying to call a nested method on your parent model.

You have:

#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

This means that you will have to call:

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

--

You're receiving a noMethod error because you're trying to call image on your Product object (which doesn't exist):

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


Fix

The fix is a little more involved than you realize.

Specifically, you have to ensure that you're able to add/create the nested Picture objects, which comes from the accepts_nested_attributes_for method:

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

This will allow you to create the appropriate form & controller actions:

#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

You'll then use 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 %>

This will allow you to save new Picture's / images with a new product, then using the code above, you'll be able to show them!!


Bonus

A pro tip I keep sharing is 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"
}

Instead of including all the styles in your Picture model, you can set them as "defaults" in your config/application.rb - which will allow you to just call has_attached_file :image, changing the styles as you require:

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

这篇关于多张图片上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆