ruby-on-rails - 获取 "undefined method ` url' 为 "":String"Using paperclip in Ruby on Rails

标签 ruby-on-rails ruby paperclip e-commerce

我在 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 :imagesImage 模型附加了名为image 的文件。因此,首先您应该使用 product.images 获取产品的所有图像,循环遍历每个图像并映射图像网址,如下所示

#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附加文件名

文件名(图像)模型名称(图像)相同会造成困惑(就像现在一样)。为了将来保持简单,请尝试使用不同的名称。例如,说头像,那么上面的代码将如下所示

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

关于ruby-on-rails - 获取 "undefined method ` url' 为 "":String"Using paperclip in Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45822730/

相关文章:

ruby-on-rails - 工厂女孩和回形针附件

ruby-on-rails - Heroku 上的 open-uri 错误

sql - 如何编写命名范围以通过传入的所有数组进行过滤,而不仅仅是通过匹配一个元素(使用 IN)

mysql - rails : ActiveRecord class throws "NameError: uninitialized constant" - Is this a naming issue?

ruby-on-rails - Mina 部署 : I have set up SSH, 但无法连接到 git@github.com

ruby - 在 Ruby 中如何确定一个字母数字 ID 大于另一个?

ruby-on-rails - Heroku,问题/问题

ruby-on-rails - 这个link_to代码应该工作吗? Ruby/Rails 4.0和2.0.0

ruby - 如何使用 Nix 而不是 Homebrew 在 OS X 上设置 Ruby?

Ruby:回形针、S3 和深度克隆