Rails Screencast - 完全停留在“fields_for”

时间:2011-03-19 11:10:12

标签: ruby-on-rails

我正在关注this simple tutorial,我按照所有步骤操作...但浏览器根本没有向我显示我放在fields_for标记内的任何内容。

<%= form_for :activity, :url => activities_path do |f| %>

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

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

  <div class="field">
    <%= f.label "Foto" %><br />
    <%= f.text_field :photo %>
  </div>
  <div class="field">
    <%= f.label "Foto per la home" %><br />
    <%= f.text_field :photoHome %>
  </div>

    <% for info in @activity.infos %>

    This is rendered<br>

      <% fields_for "activity[info_attributes][]", info do |info_form| %>

    This is not rendered<br>

        <p>
          Titolo: <%= info_form.text_field :title %>
        </p>
        <p>
          Descrizione: <%= info_form.text_field :description %>
        </p>
      <% end %>
    <% end %>
    <p><%= submit_tag "Create activity" %></p>

<% end %>

上面的代码结果如下:

enter image description here

我缺少什么?

1 个答案:

答案 0 :(得分:8)

Rails有时(好吧,通常)有点令人困惑,其中帮助者需要<% vs哪些助手想要<%=。尝试

<%= fields_for "activity[info_attributes][]", info do |info_form| %>

您所关注的教程没有,但是那是从2007年开始,而在Rails 3中,这个被改变了。有关fields_for的最新文档为here