嵌套模型轨道的字段3.2.2



我的Rails应用程序中有一些嵌套模型。我有一篇文章有mny属性。

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

现在我想在我的视图中编辑这个,所以我编辑了控制器

  # 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

还编辑了View和_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 %>

但是没有办法出现。如果我想创建一个新的模型,我看不到任何属性的输入字段。

我做错了什么?

您的fields_for行中缺少一个=。也就是说,它应该是:

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

最新更新