0

Basierend auf Railscast 196 und 197, habe ich ein verschachteltes Formular zum Hinzufügen mehrerer Illustrationen (Bildanhänge) zu meinem Artikelmodell erstellt. Ich habe eine Form wie folgt aus:Wie werden zusätzliche Informationen teilweise in eine verschachtelte Form übergeben?

<%= form_for @article, :html => { :multipart => true } do |f| %> 
    <h2>Illustrations</h2> 
    <% for illustration in @article.illustrations %>  
    <%= f.fields_for :illustrations, illustration do |builder| %> 
     <%= render :partial => "illustration_fields", 
     :locals => { :f => builder, :illustration => illustration } %> 
    <% end %> 
    <% end %> 
    <p> 
    <%= f.submit "Submit" %> 
    </p> 
<% end %> 

Und die _illustration_fields Teil sieht wie folgt aus:

<div class="fields"> 
    <p> 
    <%= f.label :illustration, "Illustration" %><br /> 
    <%= link_to_remove_fields "remove", f %><br /> 
    <%= f.file_field :illustration %> 
    </p> 
</div> 

Wenn die Abbildung bereits vorhanden ist, möchte ich es nicht in einem Image-Tag auf der Seite angezeigt werden soll. Ich habe versucht, das Bild im Teil wie folgt dargestellt:

<% unless :illustration.illustration.blank? %> 
    <%= image_tag(:illustration.illustration.url) %> 
<% end %> 

Aber es wirft einen Fehler auf:

undefined method `illustration' for :illustration:Symbol 

Die Abbildung Feld existiert auf jeden Fall in der Abbildung Modell, als ich das Bild im Inneren des Displays kann Hauptform wie folgt aus:

<% unless illustration.illustration.blank? %> 
    <%= image_tag(illustration.illustration.url) %> 
<% end %> 

es scheint, dass Rails nicht die Darstellung von Informationen in den Teil nicht versteht (oder ich vorbei es nicht richtig). Was ist der richtige Ansatz dafür?

Antwort

1

Gelöst es. Auf das Illustrationsobjekt kann von innerhalb des Partials wie folgt zugegriffen werden. Keine Notwendigkeit, es innerhalb der :locals Hash zu übergeben.

<% unless f.object.illustration.blank? %> 
    <%= image_tag(f.object.illustration.url) %> 
<% end %> 
+0

Uuhh ... eewwwww :) IMHO, es ist viel schöner, "Illustration" als lokal zu übergeben. – Zabba

0

Sie haben das Symbol Fehler, weil Sie die lokale Verwendung : im unless Zugriff sind.

Der Fehler

undefined method `illustration' for :illustration:Symbol 

bedeutet, dass Ruby für ein Verfahren sah illustration auf einer Instanz der Klasse mit dem Namen Symbol; Die Methode soll offensichtlich nicht in der Symbol-Klasse existieren.

Der Weg, auf die lokale Variable zuzugreifen, ist einfach illustration.whatever.whatever (nein : wird verwendet!).

Verwandte Themen