3

在过去的几个小时里,我一直在努力让它工作,但由于某种原因我不能。我几乎完全按照 github 存储库链接中所示的步骤进行操作。

我使用以下所有步骤创建了一个新应用程序:

# rails new demo_app
# cd demo_app/
+++ added gem 'cocoon' to the Gemfile
+++ added //= require cocoon to the application.js file
# rails g scaffold Project name:string description:string
# rails g model Task description:string done:boolean project:belongs_to
+++ added has_many :tasks to the Project model
+++ added :_destroy to the permit in my projects_controller.rb file
# bundle install

这是我的意见/项目/_form.html.erb 文件:

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

      <ul>
      <% @project.errors.full_messages.each do |message| %>
        <li><%= message %></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_field :description %>
  </div>
  <%= f.fields_for :tasks do |task| %>
    <%= render 'task_fields', :f => task %>
    <%= link_to_add_association 'Add task', f, :tasks, class: "links" %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

现在这是我的意见/项目/_task_fields.html.erb 文件:

 <div id="nested-fields">
  <div class="field">
      <%= f.label :description %><br>
      <%= f.text_field :description %>
  </div>

  <div class="field">
      <%= f.label :description %><br>
      <%= f.text_field :description %>
  </div>
  <%= link_to_remove_association 'remove task', f %>
 </div>                 

这不正是指南提到的吗?当我去创建一个新项目时,它只显示默认的名称标签、名称文本框、描述标签、描述文本框和“创建项目链接”。这是新项目表单的 HTML 输出:

<!DOCTYPE html>
<html>
<head>
  <title>DemoApp</title>
  <link data-turbolinks-track="true" href="/assets/projects.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
  <script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/projects.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/cocoon.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="uIpLnix47UNaBONCR+0SV/uz1uiulU6BHqKe5qENzHQ=" name="csrf-token" />
</head>
<body>

<h1>New project</h1>

<form accept-charset="UTF-8" action="/projects" class="new_project" id="new_project" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="uIpLnix47UNaBONCR+0SV/uz1uiulU6BHqKe5qENzHQ=" /></div>

  <div class="field">
    <label for="project_name">Name</label><br>
    <input id="project_name" name="project[name]" type="text" />
  </div>
  <div class="field">
    <label for="project_description">Description</label><br>
    <input id="project_description" name="project[description]" type="text" />
  </div>
    <div class="actions">
    <input name="commit" type="submit" value="Create Project" />
  </div>
</form>

<a href="/projects">Back</a>


</body>
</html>

有人可以帮助我吗?看起来我正在做指南中提到的所有事情,但我没有从中得到任何东西。

4

1 回答 1

11

The link_to_add_association should be located outside the fields_for tag, whatever is inside it will be replicated for each task added and you generally only want a link to add at the end, also nothing shows because no task is instantiated, in order to instantiate one you either click the link_to_add_association or instantiate them on the projects form creation, this can be done at the projects controller new action by doing

#projects_controller.rb
def new
    @project = Project.new 
    @projet.tasks.build
end

You can also instantiate multiple tasks on form creation here by doing some loop like

3.times do
  @project.tasks.build
end
于 2014-10-13T19:13:05.157 回答