在 Rails 上 Ruby 中使用回形针获取 "undefined method `url' for "":String"

getting "undefined method `url' for "":String" Using paperclip in Ruby on Rails

我在我正在构建的电子商务应用程序的app/views/cart/_cart.html.erb页面上收到此错误undefined method 'url' for "":String Did you mean? URI

如果我从代码中排除 <td><%= image_tag item.product.image.url(:thumb) %></td> 行,我就不会收到消息并且可以看到购物车中的物品,但不会看到购物车中产品的小图片。

如果有人能在这里帮助我,那就太好了,因为我是 ROR 新手,无法自己解决。

这是出现错误的 _cart.html.erb 视图

<% @cart.product_items.each do |item| %>

<table class="table">
    <tr>
        <th>Quantity</th>
        <th>Name</th>
        <th>Price &euro;</th>
        <th>Image</th>

    </tr>
    <tr>
        <td><%= item.quantity %>&times; </td>
        <td><%= item.product.title %></td>
        <td><%= number_to_currency(item.total_price_usd, :unit => '€ ', :precision => 0)%></td>
        <td><%= image_tag item.product.image.url(:thumb) %></td>

    </tr>

</table>

该应用有图片模型,看起来像这样:

class Image < ActiveRecord::Base

  belongs_to :product
  has_many :product_items, :dependent => :destroy

    has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

end

product.rb模型是这样的

class Product < ActiveRecord::Base
  acts_as_list :scope => [:category, :label]
  belongs_to :category
  belongs_to :label

  has_many :images
  accepts_nested_attributes_for :images
  has_many :product_items, :dependent => :destroy

   extend FriendlyId
   friendly_id :title, use: [:slugged, :finders]

  #before_destroy :ensure_not_product_item

    validates :title, :description, presence: true
    validates :price_usd, numericality: {greater_than_or_equal_to: 0.01}
    validates :title, uniqueness: true

end

product_item.rb模型是这样的:

class ProductItem < ActiveRecord::Base
  belongs_to :product 
  belongs_to :cart
  belongs_to :order

  belongs_to :image

  def total_price_usd
    product.price_usd * quantity
  end

end

我可以在 views/products/show 中看到产品图片,但是当我将产品添加到购物车并想查看购物车中的商品时,应用程序中断并显示错误消息:undefined method 'url' for "":String Did you mean? URI

这里是 views/products/show.html.erb

    <div class="col-xs-12 col-sm-6 center-block" >
      <div id='carousel-custom' class='carousel slide' data-ride='carousel'>
        <div class='carousel-outer'>
            <!--slider -->
            <div class='carousel-inner '>
                <div class='item active'>
                    <%= image_tag @product.images.first.image.url(:medium), 
                     class: "img-responsive", id: "" %>
                    </div>      
                    <% @product.images.drop(1).each do |image_product| %>
                       <div class='item'>
                        <%= image_tag image_product.image.url(:medium), 
                        class: "img-responsive", id: "" %>

              </div>
                <% end %>

            <script>
              $("#zoom_05").elevateZoom({ zoomType    : "inner", cursor: 
              "crosshair" });
            </script>
        </div>
        <!-- sag sol -->
        <a class='left carousel-control' href='#carousel-custom' data-
         slide='prev'>
            <span class='glyphicon glyphicon-chevron-left'></span>
        </a>
        <a class='right carousel-control' href='#carousel-custom' data-
         slide='next'>
            <span class='glyphicon glyphicon-chevron-right'></span>
        </a>
    </div>
                <!-- thumb -->
    <ol class='carousel-indicators mCustomScrollbar meartlab'>
        <li data-target='#carousel-custom' data-slide-to='0' class='active'>
            <%= image_tag @product.images.first.image.url(:medium), class: 
         "img-responsive", id: "zoom_05" %>
        </li>
        <% @product.images[1..-1].each_with_index do |image_product, index| 
           %>
            <li data-target='#carousel-custom' data-slide-to=<%= index + 1%> 
             >
                <%= image_tag image_product.image.url(:medium), class: "img-
              responsive", id: "" %>
            </li>
        <% end %>
        </li>
    </ol>
   </div>

   <script type="text/javascript">
    $(document).ready(function() {
        $(".mCustomScrollbar").mCustomScrollbar({axis:"x"});
    });
   </script>

  </div>
  </div>
  </div>

您在 Product 中有 has_many :images 个。并且 Image 模型有名为 image 的附加文件。因此,首先您应该使用 product.images 获取 产品的所有图像 ,遍历每个图像并映射图像 url,如下所示

#fetch all images of a product
<% item.product.images.each do |img| %>
  #map the url of each img
  <td><%= image_tag img.image.url(:thumb) %></td>
<% end %>

其中 imgImage 的实例,image 附加文件名.

文件名(图像)型号名称(图像)相同会使事情变得混乱(就像现在一样)。为了以后简单起见,请尝试使用不同的名称。例如,说 avatar,那么上面的代码将如下所示

#fetch all images of a product
<% item.product.images.each do |img| %>
  #map the url of each img
  <td><%= image_tag img.avatar.url(:thumb) %></td>
<% end %>