我正在构建一个项目管理应用程序。 我的系统就像Project-> Feature-> Task。 一个项目有很多功能,一个功能有很多任务。
class Project < ApplicationRecord
belongs_to :user
has_many :features
validates :title, presence: true, length: { minimum: 4 }
accepts_nested_attributes_for :features
end
class Feature < ApplicationRecord
belongs_to :project
has_many :tasks
accepts_nested_attributes_for :tasks
end
class Task < ApplicationRecord
belongs_to :feature
end
我有show action指示的项目仪表板-
在这里我有一个部分可以添加新功能,也可以在功能下添加新任务。
现在,这是views/projects/show.html.erb
<div class="col-md-3 colm" id="card2">
<div class="card">
<div class="card-body">
<a href="#card2" class="close" data-dismiss="alert" aria-label="close">×</a>
<h5 class="card-title">Backlog</h5>
<div class="card-text">
</div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
+ Add Feature
</button>
<div class="collapse" id="collapseExample">
<%= render "feature" %>
</div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseTask" aria-expanded="false" aria-controls="collapseTask">
+ Add Task
</button>
<div class="collapse" id="collapseTask">
<%= link_to "new task", new_project_feature_task_path(@project,1), id: "newlink", remote: true %>
</div>
</div>
</div>
</div>
_feature.html.erb
表单显示为
<%= form_for(@project, class: "form-group row") do |form| %>
<%= form.fields_for :features do |builder| %>
<%= builder.label :name %>
<%= builder.text_field :name, required: true %>
<%= builder.label :desc %>
<%= builder.text_field :desc %>
<%= builder.submit class: "btn btn-primary m-2" %>
<% end %>
<% end %>
但是当我检查数据库时,数据未插入features
数据库中
供参考,项目控制器类为->'
def show
@project = Project.find(params[:id])
@project.features.build
end
但是问题是,每当我渲染_feature.html.erb时,它提供的是编辑功能表单而不是新功能表单,该如何在项目展示页面中呈现新功能创建表单?
答案 0 :(得分:1)
您的代码似乎有很多潜在的错误。例如,在这里:
def show
@project = Project.find(params[:id])
@project.features.build
end
...可能希望更像是:
def show
@project = Project.find(params[:id])
@feature = @project.features.build
end
这:
<%= form_for(@project, class: "form-group row") do |form| %>
...可能希望更像是:
<%= form_for [@project, @feature], class: "form-group row" do |form| %>
假设您的features
路由嵌套在projects
路由下,例如:
resources :projects do
resources :features, shallow: true do
resources :tasks, shallow: true
end
end
您可能正在获得编辑表单,因为您要将现有记录再次传递到form_for
,在这里:
<%= form_for(@project, class: "form-group row") do |form| %>
这看起来也很可疑:
<%= link_to "new task", new_project_feature_task_path(@project,1), id: "newlink", remote: true %>
...因为将1
硬编码在new_project_feature_task_path
中似乎很奇怪。
此外,您没有显示当前功能。而且,您似乎想做类似的事情:
<div class="col-md-3 colm" id="card2">
<div class="card">
<div class="card-body">
<a href="#card2" class="close" data-dismiss="alert" aria-label="close">×</a>
<h5 class="card-title">Backlog</h5>
<div class="card-text">
</div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
+ Add Feature
</button>
<div class="collapse" id="collapseExample">
<%= render "feature" %>
</div>
<% @project.features.each do |feature| %>
# render your feature details here
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseTask" aria-expanded="false" aria-controls="collapseTask">
+ Add Task
</button>
<div class="collapse" id="collapseTask">
<%= link_to "new task", new_project_feature_task_path(@project, feature), id: "feature-#{feature.id}-new-task-link", remote: true %>
</div>
<% end %>
</div>
</div>
</div>
最后,您没有显示任何有关如何处理对new_project_feature_task_path
的ajax调用的信息。大概会生成表格吗?您是否有js
来显示表格(某种on ajax:success
或您有什么)?