gpt4 book ai didi

ruby-on-rails - 如何在索引 View 中显示图像

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:10 25 4
gpt4 key购买 nike

我正在使用 carrierwave 并尝试在索引 View 中显示产品图像。这是我的模型、 Controller 和 View

产品.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

产品附件.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。

最佳答案

我希望以下内容有效:

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

关于ruby-on-rails - 如何在索引 View 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com