简体   繁体   English

Rails不保存/不允许嵌套表格的值存储

[英]Rails not saving /permitting value of nested forms to be stored

My _feature.html.erb looks like- 我的_feature.html.erb看起来像-

<%= error_messages_for(@project) %>
<div class="card m-auto shadow-lg p-3 mb-5 bg-white rounded">
  <div class="card-body">
  <%= form_for [@project, @feature], class: "form-group inline", remote: true  do |builder| %>
      <%= builder.hidden_field :feature_token_id, value: auto_generate_id %>

      <%= builder.hidden_field :category, class: "form-control", value: category_value %>

      <%= builder.label :name %>
      <%= builder.text_field :name, required: true, class: "form-control" %>

      <%= builder.label :desc, "Description" %>
      <%= builder.text_field :desc, class: "form-control" %>

      <%= builder.fields_for :task, remote: true do |form| %>
        <div class="form-group">
          <%= form.label :name %>
          <%= form.text_field :name, required:true, class: "form-control" %>
        </div>
        <div class="form-check">
          <%= form.check_box :completed, class: "form-check-input" %>
          <%= form.label :completed %>

        </div>
        <div class="form-group">
          <%= form.label :user_id, "Select the User to Assign this task:" %><br>
          <%= form.collection_select :user_id, User.all, :id, :name %>
        </div>

      <% end %>

      <%= builder.submit class: "btn btn-primary m-2" %>

  <% end %>
  </div>
</div>

And my FeaturesContrller is -> 我的FeatureContrller是->

class FeaturesController < ApplicationController
  before_action :set_project

  def index
    @features=Feature.all
  end

  def new
    @feature=Feature.new
    @feature.tasks.new
  end

  def create
    @feature=@project.features.new(feature_params)
    binding.pry
    if @feature.save
      redirect_to @project
    else
      render "new"
    end
  end

    private
    def set_project
      @project=Project.find(params[:project_id])
    end

    def feature_params
      params.require(:feature).permit(:name, :desc, :category, :feature_token_id, tasks_attributes: [:name])
    end
end

But while I try to save the form values only the Feature table gets updated but the tasks_attributes remain unpermitted instead of permitting them, what is the wrong thing I am doing? 但是,当我尝试保存表单值时,只有Feature表得到更新,而task_attributes却不被允许而不是被允许,但我在做什么错呢?

And here is the log from pry 这是pry的日志

    13: def create
    14:   @feature=@project.features.new(feature_params)
 => 15:   binding.pry
    16:   if @feature.save
    17:     redirect_to @project
    18:   else
    19:     render "new"
    20:   end
    21: end

[1] pry(#<FeaturesController>)> feature_params
Unpermitted parameter: :task
=> <ActionController::Parameters {"name"=>"ayann", "desc"=>"uhi", "category"=>"Current Iteration", "feature_token_id"=>"38378"} permitted: true>
[2] pry(#<FeaturesController>)> 

And If I exit from pry only Feature table is updated with new value not Task table!(Log looks like) 而且,如果我退出撬动,仅功能表被更新为新值,而不是任务表!(日志看起来像)

  Feature Create (0.8ms)  INSERT INTO `features` (`name`, `desc`, `feature_token_id`, `project_id`, `created_at`, `updated_at`, `category`) VALUES ('ayann', 'uhi', '38378', 4, '2019-08-20 07:55:27', '2019-08-20 07:55:27', 'Current Iteration')

My feature.rb model -> 我的feature.rb模型->

class Feature < ApplicationRecord
  belongs_to :project
  has_many :tasks, dependent: :destroy
  accepts_nested_attributes_for :tasks
end

And task.rb model -> task.rb模型->

class Task < ApplicationRecord
  belongs_to :feature
  belongs_to :user, optional: true
end

what is the reason that my nested model task is not being saved? 嵌套模型任务未保存的原因是什么?

The problem is in the _feature.html.erb file, in building the form for accepting nested attributes for tasks. 问题在于_feature.html.erb文件中,该文件用于构建用于接受任务的嵌套属性的表单。 The line <%= builder.fields_for :task, remote: true do |form| %> <%= builder.fields_for :task, remote: true do |form| %> <%= builder.fields_for :task, remote: true do |form| %> tries to build a form for the relation task , but the Feature model has a has_many relation with tasks. <%= builder.fields_for :task, remote: true do |form| %>尝试为关系task构建表单,但是Feature模型与任务具有has_many关系。 Changing that line to builder.fields_for :tasks will pass the parameters as expected (as tasks_attributes and not task_attributes , which is probably being sent currently). 将该行更改为builder.fields_for :tasks将按预期传递参数(如tasks_attributes而不是task_attributes ,当前可能正在发送)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM