2012-03-26 90 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