【问题标题】:Submit a model's form from another model's view in Rails App在 Rails App 中从另一个模型的视图提交模型的表单
【发布时间】:2019-12-21 07:09:34
【问题描述】:

我正在构建一个项目管理应用程序。 我的系统就像项目-> 功能-> 任务。 一个项目有很多功能,一个功能有很多任务。

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

我有显示操作指示的项目仪表板- 我有一个添加新功能的部分,还有一个功能下的新任务。 现在这是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 时,它都会提供编辑功能表单而不是新功能表单,如何在项目显示页面内渲染新功能创建表单?

【问题讨论】:

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


    【解决方案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 %>
    

    ...因为在您的new_project_feature_task_path 中硬编码1 似乎很奇怪。

    另外,您没有显示当前功能。而且,您似乎想要执行以下操作:

    <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 或者你有什么)?

    【讨论】:

    • 非常感谢您的详细分析,我不能要求更多..实际上(我忘记删除的最后一行)我想渲染一个表单任务表单,它将用于添加任务到各自的特点
    • 它显示为没有路由匹配 {:action=>"index", :controller=>"tasks", :feature_id=>nil},缺少必需的键:[:feature_id]
    • 什么是“它”(当您说“它正在显示......”时)。而且,在什么情况下?错误在哪里抛出?
    • 我按照你所说的方法使用此表单标题实现功能下的任务-> &lt;%= form_for [@feature, @task], class: "form-group row" do |builder| %&gt; 渲染此表单显示错误为ActionController::UrlGenerationError in Projects#show
    • 您是否尝试在 @project.features.each 循环中执行此操作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多