views/products/show.html.erb
に写真を表示する際に問題が発生しています。私はいつもこのエラーが出る:RORアプリケーションの未定義のローカル変数またはメソッド
undefined local variable or method 'product' for <#:0x007fe0bc949fb8>
Did you mean?@product
これは私がビュー内の写真を表示するために使用しているコードです:ここ
views/products/show.html.erb
<div class="col-xs-12 col-sm-6 center-block" >
<% product.images.each do |image_product| %>
<%= image_tag image_product.image.url(:medium), class: "img-responsive" %>
<% end %>
</div>
はproducts_controller.rb
としてのためshow
方法であり、エラーメッセージが表示された後に、@products
を追加した後です。ここ
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def show
@products = Product.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :price_usd, :image, :category_id, :stock_quantity, :label_id, :query, :slug, images_attributes: [:image , :id , :_destroy])
end
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
end
、ここでは、私はこのエラーを取得していますなぜ、誰かが助言してくださいすることができますかわからないimage.rb
モデル
class Image < ActiveRecord::Base
belongs_to :product
has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
あるのですか?
showメソッドで変数を設定する前にコールバックを使用していますか?コントローラ全体を追加できますか? –
はい@SebastiánPalma私が追加したコントローラーを見てください。私はbefore_action:set_productのみを使用しています:[:show、:edit、:update、:destroy] 'これはどのように影響しますか?何をすべきか? – Slowboy
すべてのレコードを取得するために、showメソッドで何をしたいですか?そのような場合には '@ products'変数を削除し、' set_product'メソッド( '@product.images.each 'など)からの' @ product'の画像を繰り返し処理することができます。 .. ' –