0

我正在使用简单表格。我有一个用于创建新项目的表单和一个用于编辑现有项目的表单。我还有每个项目的两个文件字段。让我烦恼的是,在创建新项目时文件字段显示得很好,但是在编辑现有项目时根本不会生成它们。

我在 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 来存储图像。这是资产类的代码:

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 回答