Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails不保存嵌套属性_Ruby On Rails_Ruby_Nested Attributes_Fields For - Fatal编程技术网

Ruby on rails Rails不保存嵌套属性

Ruby on rails Rails不保存嵌套属性,ruby-on-rails,ruby,nested-attributes,fields-for,Ruby On Rails,Ruby,Nested Attributes,Fields For,我有表格任务和项目。我有一个Item表单,其中记录了我的任务可能具有的所有可能的项,这很好。然后,我有一个Task表单,其中所有项目都显示在一个字段旁边,以便为每个项目输入成本值。这将导致Task和Item之间的联接:TaskItem(此表包含Task\u id、Item\u id和cost) 当我提交表单时,它会保存任务,但不会保存关联的任务项。我不知道我遗漏了什么,因为我为这个问题搜索了很多,但似乎什么都不起作用。请看下面的代码 型号: class Task < Application

我有表格任务和项目。我有一个Item表单,其中记录了我的任务可能具有的所有可能的项,这很好。然后,我有一个Task表单,其中所有项目都显示在一个字段旁边,以便为每个项目输入成本值。这将导致Task和Item之间的联接:TaskItem(此表包含Task\u id、Item\u id和cost)

当我提交表单时,它会保存任务,但不会保存关联的任务项。我不知道我遗漏了什么,因为我为这个问题搜索了很多,但似乎什么都不起作用。请看下面的代码

型号:

class Task < ApplicationRecord
    has_many :task_items
    has_many :items, :through => :task_items

    accepts_nested_attributes_for :task_items, :allow_destroy => true
end

class Item < ApplicationRecord
    has_many :task_items
    has_many :tasks, :through => :task_items
end

class TaskItem < ApplicationRecord
    belongs_to :task
    belongs_to :item

    accepts_nested_attributes_for :item, :allow_destroy => true
end
我的看法是:

<%= form_for :task, url:tasks_path do |f| %>
<p>
    <%= f.label :title %><br>
    <%= f.text_field(:title, {:class => 'form-control'}) %><br>
</p>

<% @items.each do |item| %>
    <% @task_items = TaskItem.new %>
    <%= f.fields_for :task_items do |ti| %>
        <%= ti.label item.description %>
        <%= ti.text_field :cost %>
        <%= ti.hidden_field :item_id, value: item.id %>
    <% end %>
<% end %>

<p>
    <%= f.submit({:class => 'btn btn-primary'}) %>
</p>



“表单控件”})%>

'btn btn primary'})%%>


您需要在类任务中的
has-many
方法中添加
inverse\u of
选项:

class Task < ApplicationRecord
  has_many :task_items, inverse_of: :task
  has_many :items, through: :task_items

  accepts_nested_attributes_for :task_items, :allow_destroy => true
end
控制器代码:

def index
end

def new
  @items = Item.all
  @task = Task.new
end

def create
  @task = Task.new(task_params)
  @task.save
  redirect_to action: "index"
end

private

def task_params
  params.require(:task).permit(:id, :title, task_items_attributes: [:id, :item_id, :cost])
end

当您尝试保存一个
TaskItem
时,日志输出是什么?使用render-plain:params[:task]。检查我得到了以下信息:“Teste 1”、“task\u items”=>{“item\u id”=>“4”、“cost”=>“55”}允许:false>“task”=>{“title”=>“Teste565656”、“task\u items”=>{“item\u id”=>“4”、“cost”=>“55”},“commit”=>“save Contato”}未允许的参数:task\u items(0.0ms)开始事务SQL(21.0ms)插入“任务”(“标题”、“创建时间”、“更新时间”)值(?,,,?)[“标题”、“测试565656”],[“创建时间”,2017-03-09 19:31:41 UTC],“更新时间”,2017-03-09 19:31:41 UTC]](122.5ms)重定向到您的提交事务您的
任务参数中有一个错误
错误:它应该声明:cost而不是:valor根据视图中的代码抱歉,任务参数已修复,但这不是问题的原因。不知何故,在保存我的任务时,任务项属性没有被理解。它仍然在我的日志中显示“Unpermitted parameter:task\u items”,并且任务项没有被创建。我应该使用其他任何地方的反向吗?我是否正确使用了_属性?非常感谢
<%= form_for @task do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field(:title, {:class => 'form-control'}) %><br>
  </p>

  <% @items.each do |item| %>
    <% task_item = @task.task_items.build %>
    <%= f.fields_for :task_items, task_item do |ti| %>
      <%= ti.label item.description %>
      <%= ti.text_field :cost %>
      <%= ti.hidden_field :item_id, value: item.id %>
    <% end %>
  <% end %>

  <p>
    <%= f.submit({:class => 'btn btn-primary'}) %>
  </p>  
<% end %>
def index
end

def new
  @items = Item.all
  @task = Task.new
end

def create
  @task = Task.new(task_params)
  @task.save
  redirect_to action: "index"
end

private

def task_params
  params.require(:task).permit(:id, :title, task_items_attributes: [:id, :item_id, :cost])
end