2010-12-05 9 views
2

Ich versuche derzeit, eine Fotogalerie-Anwendung zu erstellen, bei der nur über eine Galerie-Benutzeroberfläche auf Fotos zugegriffen werden kann. Galerie => has_many: Fotos, Foto => gehört zu: Galerie. All das funktioniert gut.Schienen und Büroklammer mit verschachtelten Modellen: Uploads vom Formular funktioniert nicht

Allerdings versuche ich nun, meinen Fotos eine angehängte Datei zu geben: image. Ich habe alles getan, was Neath in his tutorial sagt, und ich habe gerade validates_attachment_presence: image hinzugefügt. Vor der Validierung funktionierte das Fotomodell einwandfrei, außer dass nach dem Speichern eines Bildes das Bild nicht angezeigt wurde. Jetzt, mit der Validierung, nach Auswahl eines Bildes zu laden, erhalte ich ein: flash =>

1 error prohibited this photo from being saved 

There were problems with the following fields: 

    * Image file name must be set. 

Also, was ist hier los? Relevante Code unten:

Modelle/Foto

class Photo < ActiveRecord::Base 
    attr_accessible :gallery_id, :name, :rating 

    belongs_to :gallery 
    validates_associated :gallery 

    has_attached_file :image 
    validates_attachment_presence :image 

    end 

Ansichten/Fotos/_form.html.erb

<% form_for [@gallery, @photo], :html => { :multipart => true } do |f| %> 
    <%= f.error_messages %> 
    <p> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </p> 
    <p> 
    <% if @photo.image? %> 
     <%= image_tag @photo.image.url %><br /> 
     <%= link_to @photo.image.url, @photo.image.url %> 
    <% end %> 
    <%= f.label :image %><br /> 
    <%= f.file_field :image %> 
    </p> 
    <p><%= f.submit %></p> 
<% end %> 

models/gallery.rb

class Gallery < ActiveRecord::Base 
    attr_accessible :name, :user_id, :shoot_date 

    # destroy all photos when a gallery is destroyed 
    has_many :photos, :dependent => :destroy 

end 

Ich glaube, ich Ich habe das mehrteilige Formular richtig eingerichtet, und ich glaube, ich hatte dieses Problem schon vorher, als ich ein Büroklammermodell ohne Schachtelmodelle ausprobierte. Fehle ich etwas?

Update: hier ist die Mischlings Ausgabe der versucht Upload-Transaktion bilden:

Processing PhotosController#update (for 127.0.0.1 at 2010-12-05 14:19:29) [PUT] 
    Parameters: {"photo"=>{"name"=>"blah", "image"=>#<File:/tmp/RackMultipart20101205-2909-wo2g7z-0>}, "commit"=>"Save changes", "id"=>"10", "gallery_id"=>"3"} 
    Gallery Columns (0.6ms) SHOW FIELDS FROM `galleries` 
    Gallery Load (0.1ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3) 
    Photo Columns (0.7ms) SHOW FIELDS FROM `photos` 
    Photo Load (0.1ms) SELECT * FROM `photos` WHERE (`photos`.`id` = 10 AND (`photos`.gallery_id = 3)) 
WARNING: Can't mass-assign these protected attributes: image 
    SQL (0.1ms) BEGIN 
    CACHE (0.0ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3) 
    SQL (0.1ms) ROLLBACK 
Rendering template within layouts/application 
Rendering photos/edit 
Rendered photos/_form (64.1ms) 
Completed in 83ms (View: 67, DB: 2) | 200 OK [http://localhost/galleries/3/photos/10] 

Antwort

3

figured it out. Warf die Mischlings Ausgabe in google und hier gehen wir:

http://railsforum.com/viewtopic.php?id=35544

Grundsätzlich sie vergessen, Ihnen zu sagen, hinzuzufügen: Bild zum attr_accessible Auflistung.

Photo model changed to 
class Photo < ActiveRecord::Base 
    attr_accessible :gallery_id, :name, :rating, :image 

    belongs_to :gallery 
    validates_associated :gallery 

    has_attached_file :image 
    validates_attachment_presence :image 

    end 
Verwandte Themen