在Rails App中从另一个模型的视图提交模型的表单

时间:2019-08-14 17:38:26

标签: ruby-on-rails forms model-view-controller ruby-on-rails-5

我正在构建一个项目管理应用程序。 我的系统就像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">&times;</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">
          &plus; 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">
          &plus; 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时,它提供的是编辑功能表单而不是新功能表单,该如何在项目展示页面中呈现新功能创建表单?

1 个答案:

答案 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">&times;</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">
          &plus; 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">
            &plus; 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或您有什么)?