2012-04-16 140 views
0

我正在使用Simple Form。我有一個用於創建新項目的表單和一個用於編輯現有項目的表單。我也有每個項目的兩個文件字段。讓我感到困惑的是創建新項目時文件字段顯示正常,但編輯現有項目時根本不會生成它們。Rails file_field helper不返回任何東西

我在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。

完全相同的代碼在創建新項目時起作用,但在編輯現有項目時不起作用。

資產和收據都使用回形針來存儲圖像。下面是資產類代碼:

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 

回答

1

也許你忘記添加這行代碼在你的項目模型:

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

謝謝,缺少「=」是它!我有點認爲它會起作用,但似乎在Rails 3.2中規則更加嚴格。 – 2012-04-16 14:50:13