我一直在努力争取在过去的几个小时内完成这项工作,但我出于某种原因无法做到这一点。我几乎完全按照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
这是我的views / projects / _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>
这不完全是指南提到的吗?当我去创建一个新项目时,它只显示默认的名称标签,名称文本框,描述标签,描述文本框和&#34;创建项目链接&#34;。这是新项目表单的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="✓" /><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>
有人可以帮助我吗?看起来我正在做指南中提到的所有事情,而且我没有从中获得任何东西。
答案 0 :(得分:9)
link_to_add_association
应该位于fields_for
标记之外,其中的任何内容都将为每个添加的任务进行复制,您通常只需要在最后添加一个链接,也没有任何显示,因为没有任务被实例化,为了实例化一个,你可以单击link_to_add_association
或者在项目表单创建时实例化它们,这可以通过项目控制器new
动作来完成
#projects_controller.rb
def new
@project = Project.new
@projet.tasks.build
end
您还可以通过执行某些循环(例如
)在表单创建上实例化多个任务3.times do
@project.tasks.build
end