2012-04-12 26 views
0

我下面這個軌轉換爲創建嵌套表格嵌套形式:在更新時,這個詞 http://railscasts.com/episodes/196-nested-model-form-part-1Rails的使用accepts_nested_attributes_for

我有具有圖像的「字」模型,我想更新的形象好。我正在使用rails 3.2.3。

我的問題是圖像窗體不顯示在頁面上。

word.rb:

class Word < ActiveRecord::Base 

    attr_accessible :name 
    validates_presence_of :name 

    belongs_to :category 
    attr_accessible :category_id 

    has_one :image 
    accepts_nested_attributes_for :image, :allow_destroy => true 
    attr_accessible :image_attributes 

end 

字/ _form.html.erb:

<%= form_for @word, :html => {:multipart => true} do |f| %> 
    <% if @word.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@word.errors.count, "error") %> prohibited this word from being saved:</h2> 

      <ul> 
      <% @word.errors.full_messages.each do |msg| %> 
       <li><%= msg %></li> 
      <% end %> 
      </ul> 
     </div> 
    <% end %> 

    <div class="field"> 
     <%= f.label :category %> 
     <br/> 
     <%= f.collection_select(:category_id, Category.all, :id, :name) %> 
     <br/> 
     <%= f.label :name %> 
     <br/> 
     <%= f.text_field :name %> 
     <br/> 
     <%= image_tag @word.image.file_url(:thumb).to_s unless @word.image.nil? %> 
     <%= f.fields_for :image do |builder| %> 
      <br/> 
      <%= builder.label :file, "Image" %> <br/> 
      <%= builder.file_field :file %> 
      <%= builder.hidden_field :file_cache %> 
     <% end %> 
    </div> 
    <div class="actions"> 
     <%= f.submit %> 
    </div> 
<% end %> 

任何幫助是極大的讚賞,乾杯!

回答

0

問題通過刪除舊「字」的條目並添加

@word.build_image 

在words_controller「新」的行動來解決。

相關問題