2012-03-26 64 views
2

我在我的Rails應用程序中有一些嵌套模型。 我有一個文章帽子有mny屬性。Fields_for嵌套模型導軌3.2.2

class Article < ActiveRecord::Base 
    has_many :properties, :dependent => :destroy 
    accepts_nested_attributes_for :properties 
end 

class Property < ActiveRecord::Base 
    belongs_to :article 
end 

,現在我想,所以我editet的CONTROLER

# GET /articles/new 
    # GET /articles/new.json 
    def new 
    @article = Article.new 
    3.times { @article.properties.build } 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @article } 
    end 
    end 

編輯這個在我看來,並還編輯了查看和_format.html.erb

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

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

    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br /> 
    <%= f.text_area :description %> 
    </div> 
    <% f.fields_for :properties do |prop| %> 
      <div class="field"> 
      <%= prop.label :name %><br /> 
      <%= prop.text_field :name %> 
     </div> 
    <% end %> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

但有是沒有辦法出現。如果我想創建一個新的模型,我看不到任何屬性的輸入字段。

我做錯了什麼?

回答

4

您在fields_for行中缺少=行。也就是說,它應該是:

<%= f.fields_for :properties do |prop| %> 
+0

就是這樣。非常感謝。 – Lailo 2012-03-26 10:58:04

+0

不用擔心。不要忘記你也可以通過點擊綠色複選標記來接受這個正確答案;提高您的接受率會讓其他人更願意在未來幫助您。 – Chowlett 2012-03-26 11:06:11