2012-04-16 18 views
0

私はSimple Formを使用しています。私は新しいアイテムを作成するためのフォームと、既存のアイテムを編集するためのフォームを持っています。私はまた、すべての項目の2つのファイルフィールドを持っています。バグというのは、新しい項目を作成するときにファイルフィールドがうまく表示されるということですが、既存の項目を編集するときには生成されません。Rails file_fieldヘルパーは何も返しません

これは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ではルールがさらに厳しくなっているようです。 –

関連する問題