6

Railsで商品ページを作ろうとしています。これには、複数の画像とテキスト フィールドの追加が含まれます。製品用に 1 つのモデルと写真用に 1 つのモデルがあります。写真のアップロードには paperclip gem を使用しています。しかし、製品ページを表示しても画像が表示されません。写真がデータベースに保存されていません。

PS私はHAMLを使用しています。

アプリ/ビュー/製品/show.html.haml

  %b Name
  = @product.name
  %br

  %b Description
  = @product.description

  %br
  - @product.photos.each do |photo|
  = image_tag photo.image.url

アプリ/コントローラー/products_controller

class ProductsController < ApplicationController
  before_filter :require_login
    before_filter :current_user, only: [:create, :destory]

  def new 
    @product = Product.new
    @photo = Photo.new
    5.times { @product.photos.build }
  end

  def create

  @photo = current_user.photos.build(params[:photo])
  @product = current_user.products.build(params[:product])
    if @product.save
        render "show", :notice => "Sale created!"
    else
        render "new", :notice => "Somehting went wrong!"
    end
end

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

アプリ/モデル/写真

class Photo < ActiveRecord::Base
  attr_accessible :product_id    
  belongs_to :product
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "300x300>",
      :large => "600x600>"
        }
end

アプリ/モデル/製品

class Product < ActiveRecord::Base
  attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos
  belongs_to :user
end

ユーザーモデル

   class User < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation, :name

      attr_accessor :password
      has_many :products, dependent: :destroy
      has_many :photos,:through=>:products

アプリ/製品/new.html.haml

= form_for @product, :html => { :multipart => true } do |f|
  %p
    = fields_for :photos do |f_i|
      =f_i.file_field :image 
4

5 に答える 5

3

あなたは写真モデルの下に次のように書いていました:

has_many :photos, :through => :products

これは意味がありません..あなたの写真モデルに書き込みます

belongs_to :product

製品モデルで次のように記述します。

has_many :photos

実際、これは私が理解している限り、1対多の関係になります:

製品モデルに has_attached_file を記述する必要はありません。これは、写真モデルの下に次のように書き込まれます

has_attached_file :image

これで、製品の複数の写真ができました。そのため、これらの写真を表示するためのループが必要です。たとえば、ショービューで何かをします

<% unless @product.photos.blank? %>
  <% @product.photos.each do |photo| %>
    <%= image_tag photo.image.url %>
  <% end %>
<% end %>

__ _ __ _ _編集 2 _ __ _ __ _ _

製品モデルで

has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
attr_accessible :photos_attributes

あなたの写真モデルで

belongs_to :product
attr_accessible :product_id

製品コントローラーで

def new 
  @product = Product.new
  5.times { @product.photos.build }
end

def create
  @product = Product.new(params[:product])
  @product.user_id = current_user.id
  if @product.save
      render "show", :notice => "Sale created!"
  else
      render "new", :notice => "Somehting went wrong!"
  end
end

あなたのnew.html.hamlで

= form_for @product, :html => { :multipart => true } do |f|
  - if @product.errors.any?
    .error_messages
      %h2 Form is invalid
      %ul
        - for message in @product.errors.full_messages
          %li
            = message
  %p
    = f.fields_for :photos do |f_i|
      =f_i.file_field :image 

今すぐ試してみてください。!

于 2013-05-22T12:13:44.140 に答える
3

最初にあなたのフォームが間違っています.qはfields_forを使用して写真を登録し、次にfields_for f.object.photosを使用するか、Photo.new do |を使用します。g |、あなたのリレーションシップ モデルで他のモデルが間違っています has_attached_file は has_many の写真ですが、has_attached_file の Paperclip はモデルで使用するのが適切であり、他のモデルとのリレーションシップでは使用できません。これが役に立てば幸いです。いくつかの写真を含む製品を手に入れるために、宝石の繭を使用することをお勧めします。あなたのケースに応じて、https://github.com/nathanvda/cocoon

于 2013-05-11T19:38:19.380 に答える
1

あなたのコードを見た後。Product に画像属性がまったくないことは非常に簡単です。写真にはイメージ属性があります。

したがって、製品にアクセスする必要があります->写真->画像

コントローラーのショーアクションでこれを行います

@product = Product.find(id)
@photos = @product.photos

show.html.erb で

-@photos.each do |photo|
= image_tag photo.image.url
-end

Haml 構文については、最近 html.erb を使用してプロジェクトに取り組んでいるため、私のアプリケーションです。SO エラーがある場合は、haml 構文を修正します。

于 2013-05-23T07:37:40.147 に答える
-1
   @product.image requires image to be the attribute of product model means image fields should be in products table.

   @product.image should be @product.photo.image.url
于 2013-05-22T09:20:55.327 に答える
-1

おもう

5.times { @product.photos.build }

@fields_for と対話する #new メソッド cuz .build メソッドにある必要があります

于 2013-05-12T04:33:10.420 に答える