如何在索引视图中显示图像

时间:2018-06-06 19:27:43

标签: ruby-on-rails ruby ruby-on-rails-4

我正在使用carrierwave并尝试在索引视图中显示产品图片。这是我的模型,控制器和视图

product.rb

 class Product < ActiveRecord::Base
   has_many :order_items
   belongs_to :category, required: false
   has_many :product_attachments
   accepts_nested_attributes_for :product_attachments

   mount_uploader :image, ImageUploader
   default_scope { where(active: true) }
end

product_attachment.rb

   class ProductAttachment < ApplicationRecord
     mount_uploader :image, ImageUploader
     belongs_to :product
   end

products_controller.rb(摘录)

class ProductsController < ApplicationController
  def index
  @products = Product.all
  @order_item = current_order.order_items.new
end


def show
  @product = Product.find(params[:id])
  @product_attachments = @product.product_attachments.all
end

def new
  @product = Product.new
  @product_attachment = @product.product_attachments.build
  @categories = Category.all.map{|c| [ c.name, c.id ] }
end

def create
  @product = Product.new(product_params)
  @product.category_id = params[:category_id] 

  respond_to do |format|
  if @product.save
    params[:product_attachments]['image'].each do |a|
      @product_attachment = @product.product_attachments.create!(:image => a,     :product_id => @product.id)
    end
    format.html { redirect_to @product, notice: 'Product was successfully created.' }
  else
    format.html { render action: 'new' }
   end
  end
end

private
  def product_params
    params.require(:product).permit(:name,:price, :active,  :description, product_attachments_attributes: 
[:id, :product_id, :image], category_attributes: [:category_id, :category])
   end
end

index.html.erb

<div class="row">
  <div class="col-xs-offset-1 ">
   <% @products.each do |product| %>
     <%= render "product_row", product: product, order_item: @order_item %>
   <% end %>
</div>

_product_row.html.erb

<div class="well">

  <div class="row">

  <div class="container-fluid row">
  <div class="col-md-5 col-lg-5 col-sm-5 col-xs-5 binder">
    <br><%= image_tag product.image_url.to_s %><br><br>
 </div>
  <div class="col-md-4 col-lg-4 col-sm-4 col-xs-4 binder">
    <h4 class="text-left"><%= product.name.split.map(&:capitalize).join(' ') %> </h4>
    <h4 class="text-left"><span style="color: green"><%= number_to_currency(product.price, :unit => "€") %></span></h4>
    <h4><%= link_to Category.find(product.category_id).name, category_path(product.category_id) %></h4>
    <h6 class="text-left"><%= link_to 'Delete', product_path(product), method: :delete,
          data: { confirm: 'Are you sure?' } %></h6><br><br>
  </div>

 <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3 binder">

  <%= form_for order_item, remote: true do |f| %>
    <div class="input-group">
      <%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
      <div class="input-group-btn">
        <%= f.hidden_field :product_id, value: product.id %>
        <%= f.submit "Add to Cart", class: "btn btn-primary text-right" %>
      </div>
    </div>
  <% end %>
 </div>
 </div>

 </div>
</div>

使用<%= image_tag product.image_url.to_s %>时,图片不会显示。当我将其更改为<%= image_tag product_attachments.first.image_url.to_s %>时,我收到以下错误:

 undefined local variable or method `product_attachments' for #<#<Class:0x00007f28544ccc68>:0x00007f285dd3aab8>

我是Ruby的新手,不知道我做错了什么或如何解决这个问题。任何帮助,将不胜感激。我在ubuntu上使用Ruby版本2.5.1和rails 5.2.0。

3 个答案:

答案 0 :(得分:2)

我希望以下工作:

<%= image_tag product.product_attachments.first.image_url.to_s %>

答案 1 :(得分:1)

如果我理解您的代码段,您安装ImageUploader的模型为ProductAttachment(其属性为image),因此您可以删除mount_uploader :image, ImageUploader模型的Product

对于一种产品,图像将安装在每个product_attachments上。只需通过遍历product_attachments显示部分内的图像:

<% product.product_attachments.each do |attachment| %>
  <%= image_tag(attachment.image.url) %>
<% end %>

答案 2 :(得分:1)

试试这个:

image_tag(product.product_attachments.first.image.url.to_s)

它应该工作。我意识到有时image_url不能按预期工作,但image.url会这样做。