Lew*_*uce 3 ruby-on-rails cocoon-gem
我一直在努力争取让这个工作在最后几个小时,我出于某种原因我不能.我几乎完全遵循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
Run Code Online (Sandbox Code Playgroud)
这是我的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 %>
Run Code Online (Sandbox Code Playgroud)
现在这是我的views/projects/_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>
Run Code Online (Sandbox Code Playgroud)
这不完全是指南提到的吗?当我去创建一个新项目时,它只显示默认的名称标签,名称文本框,描述标签,描述文本框和"创建项目链接".这是新项目表单的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>
Run Code Online (Sandbox Code Playgroud)
有人可以帮我这个吗?看起来我正在做指南中提到的所有事情,而且我没有得到任何东西.
小智 11
它link_to_add_association应该位于fields_for标记之外,它内部的任何内容都会被复制,因为每个添加的任务都通常只需要在最后添加一个链接,也没有任何显示,因为没有任何实例被实例化,为了实例化你要么点击一个link_to_add_association或者在项目表单创建时实例化它们,这可以new通过执行项目控制器操作来完成
#projects_controller.rb
def new
@project = Project.new
@projet.tasks.build
end
Run Code Online (Sandbox Code Playgroud)
您还可以通过执行某些循环来在表单创建时实例化多个任务
3.times do
@project.tasks.build
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2209 次 |
| 最近记录: |