2013-05-11 13 views
6

Ich versuche, eine Produktseite in Schienen zu erstellen. Dies beinhaltet das Hinzufügen mehrerer Bilder und Textfelder. Ich habe ein Modell für Produkte und eins für Fotos. Ich benutze den Büroklammer-Edelstein zum Hochladen von Fotos. Aber ich bekomme kein Bild, wenn ich die Produktseite ansehe. Fotos werden nicht in der Datenbank gespeichert.Schienen - Büroklammer - Mehrere Fotos hochladen nicht speichern

P.S. Ich benutze HAML.

app/views/products/show.html.haml

%b Name 
    = @product.name 
    %br 

    %b Description 
    = @product.description 

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

app/controllers/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 

app/models/Foto

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

app/models/Produkt

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 

Benutzermodell

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

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

app/products/new.html.haml

+0

Diese Zeile entfernen ** has_attached_file: photo ** von Ihrem Produktmodell gibt es hier keine Notwendigkeit. –

+0

ich verstehe bisher nicht, warum Sie die gleiche Frage noch einmal stellen ... Was ist mit alten? Sie verschwenden andere Zeit .. http://stackoverflow.com/questions/16665809/ruby-on-rails-paperclip-not-saving-to-database –

+0

und dritte ist hier http://stackoverflow.com/ Fragen/16807112/Rails-Büroklammer-ist-nicht-Addieren-My-Image –

Antwort

3

zuerst Ihre Form ist falsch, q für die Registrierung von Fotos verwendet fields_for, dann verwenden Sie fields_for fobjec t.photos oder verwenden Sie Photo.new do | g |, die andere in Ihrem Beziehungsmodell ist falsch has_attached_file ist has_many Fotos, die has_attached_file Paperclip ist geeignet, in dem Modell verwendet werden, nicht in der Beziehung mit dem anderen Modell verwendet werden. Hoffe, das hilft, jetzt ein Produkt mit mehreren Aufnahmen zu haben, empfehle ich Ihnen den Edelstein Kokon verwenden, I q geht nach Ihrem Fall https://github.com/nathanvda/cocoon

+0

Dies ist nicht artikulierbar genug –

-1

Ich denke

5.times { @product.photos.build } 

in #new Methode sollte cuz .build Methode interagieren mit @fields_for

+0

Seine Beziehung zwischen Modellen vermasselt. –

-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 
+0

Hat nicht funktioniert ich am Ende bekomme undefinierte Methode 'Fotos 'für Nil: NilClass –

+0

gibt es mehrere Fotos für Produkt .. @ product.photo wird nicht funktionieren, ich denke, brauche eine Schleife für die Anzeige aller Fotos! –

3

Sie hatte unter Foto-Modell geschrieben als:

has_many :photos, :through => :products 

Dies macht keinen Sinn .. in Ihrem Foto-Modell

belongs_to :product 

während in Produktmodell Schreib schreiben:

has_many :photos 

dieser Eins-zu-viele sein wird tatsächlich Beziehung so weit ich hatte verstanden:

Es gibt keine Notwendigkeit, has_attached_file im Produktmodell zu schreiben.Dies wird unter Fotomodell wie

has_attached_file :image 

geschrieben Jetzt haben Sie mehrere Fotos für das Produkt. Sie brauchen also eine Schleife, um diese Fotos anzuzeigen. zB tun in Ihrer Show Ansicht irgendeine Sache

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

__ _ __ _ _ EDIT 2 _ __ _ __ _ _

in Ihr Produktmodell

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

in Ihrem Fotomodell

belongs_to :product 
attr_accessible :product_id 

in Ihrer Produkte Controller

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 

in Ihrem 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 

versuchen nun heraus. !

+0

okay, also ich habe das in, aber in Show es zeigt nie irgendwelche Bilder. –

+0

Fotos speichern in db? –

+0

huh ... das ist seltsam sie sind nicht –

1

Nach dem Blick auf Ihren Code. Es ist ziemlich einfach, dass das Produkt überhaupt kein Bildattribut hat. Foto habe Bildattribut.

So haben Sie Produkte zugreifen -> Fotos -> Bilder

Tun Sie dies in Show Controller-Aktion

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

In show.html.erb

[email protected] do |photo| 
= image_tag photo.image.url 
-end 

Für haml Syntax meine Applogies wie in diesen Tagen arbeiten auf Projekt mit html.erb. SO korrigiert haml Syntax, wenn es einen Fehler gibt.

Verwandte Themen