0

シンプルフォームを使用しています。新しいアイテムを作成するためのフォームと、既存のものを編集するためのフォームがあります。また、すべてのアイテムに 2 つのファイル フィールドがあります。私を悩ませているのは、新しいアイテムを作成するときにファイルフィールドが正常に表示されることですが、既存のアイテムを編集するときにそれらがまったく生成されないことです。

これはRails 3.0で完全に機能していましたが、Rails 3.2.1では機能しません。

フォーム:

<%= simple_form_for @item, :html => { :multipart => true } do |f| %>
    <%= f.input :title, :input_html => { :maxlength => 35 } %>
    <%= f.input :description, :input_html => { :maxlength => 450 } %>
    <%= f.input :secure_details, :placeholder => "Serial numbers and other stuff that will remain private", :input_html => { :maxlength => 450 } %>
    <%= f.association :bookmark, :collection => current_user.bookmarks(:order => :position), :include_blank => false %>
    <%= f.input :warranty_until, :as => :string, :input_html => { :id =>'datepicker2' } %>
    <div class="image_attachment">
        <div class="attachment_text">
            Update item photo<br />
            <small>(will replace old one)</small>
        </div>
        <div class="attachment_button">
        <% f.fields_for :assets do |asset| %>
            <%= asset.file_field :photo %>
        <% end %>
        </div>
    </div>
    <div class="image_attachment">
        <div class="attachment_text">
            Update receipt<br />
            <small>(will replace old one)</small>
        </div>
        <div class="attachment_button">
        <% f.fields_for :receipts do |receipt| %>
            <%= receipt.file_field :photo %>
        <% end %>
        </div>
    </div>
    <%= f.input :public, :label => "My friends can see this item", :input_html => { :class => "right" } %>
    <%= f.input :giveaway, :label => "Mark as giveaway", :input_html => { :class => "right" } %>
    <div class="margin_r margin_t">
        <%= f.button :submit, :class => 'small_button white right' %>
    </div>

<% end %>

基本的に、コードのこの部分は機能しません:

<div class="attachment_button">
        <% f.fields_for :assets do |asset| %>
            <%= asset.file_field :photo %>
        <% end %>
</div>

生成された HTML は単なる空の div です。

新しいアイテムを作成するときはまったく同じコードが機能しますが、既存のものを編集するときは機能しません。

Assets と Receipts の両方で、画像の保存に Paperclip を使用しています。Asset クラスのコードは次のとおりです。

class Asset < ActiveRecord::Base
    belongs_to :item

    has_attached_file :photo, 
        :styles => {
            :thumb => "80x80#",  
            :small => "150x150>" }
    validates_attachment_size :photo, :less_than => 550.kilobytes
    validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

end
4

1 に答える 1

1

Item モデルに次のコード行を追加するのを忘れている可能性があります。

accepts_nested_attributes_for :receipts, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true

「=」を追加します。

<%= f.fields_for :assets do |asset| %>
    <%= asset.file_field :photo %>
<% end %>

<%= f.fields_for :receipts do |receipt| %>
    <%= receipt.file_field :photo %>
<% end %>
于 2012-04-16T14:18:48.263 に答える