【问题标题】:RoR: Nested Forms Model attributes inside other model attributesRoR:其他模型属性中的嵌套表单模型属性
【发布时间】:2015-07-10 18:41:03
【问题描述】:

我正在制作一个项目,其中“项目有_many 个任务,TASK 有 Task1,task1 有任务 2,task2 有 task3。”

但我坚持以一种形式插入所有这些任务,

我使用嵌套形式的 gem,

这是我认为可行的。

def task_params
  params.require(:task).permit(:projeto_id, :raiz, :descr, :hour, :typo, :tsk1s_attributes => [:raiz, :descr, :hour, :typo, :_destroy, :tsk2s_attributes => [:raiz, :descr, :hour, :typo, :_destroy, :tsk3s_attributes => [:raiz, :descr, :hour, :typo, :_destroy]]])
end

task.erb 模型

class Task < ActiveRecord::Base
 has_many :tsk1s
  belongs_to :projeto
   accepts_nested_attributes_for :tsk1s, allow_destroy: true
 end

tsk1.erb 模型

class Tsk1 < ActiveRecord::Base
 belongs_to :task
  has_many :tsk2s
   accepts_nested_attributes_for :tsk2s, allow_destroy: true
end

tsk2.erb 相同,tsk3 只属于_to

下面是我的一张表格,这是用于添加 tsk2,tsk3 和 tsk1 相同。

<%= f.fields_for :tsk2s do |tsk2| %>
   <ul class="step">
   <li>
   <%=  tsk2.number_field :raiz, :placeholder => "Identificador" %> 
   <%=  tsk2.text_field :descr, :placeholder => "Descrição" %> 
   <%=  tsk2.number_field :hour, :placeholder => "Carga Horária" %> 
   <%= tsk2.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %>
     <%= tsk2.link_to_remove "-" %>  
    <%= f.link_to_add "+Galho", :tsk3s %> </li>
   <% end %>

这实际上只保存了task和tsk1属性,

任务控制器:

 def create
@task = Task.new(task_params)

respond_to do |format|
  if @task.save
    format.html { redirect_to @task, notice: 'Task was successfully created.' }
    format.json { render :show, status: :created, location: @task }
  else
    format.html { render :new }
    format.json { render json: @task.errors, status: :unprocessable_entity }
  end
end

结束

 def new
@task = Task.new

结束

完整的表格代码http://pastebin.com/DSwZGfNs

【问题讨论】:

  • 请使用控制器的newcreate 方法更新您的帖子。并且还带有完整的表单代码。

标签: jquery ruby-on-rails nested-forms


【解决方案1】:

其中一个原因可能在于您的表单代码,:tsk2s:tsk3s 嵌套在 tasks 中,但根据模型代码,:tsk2s 应该嵌套在:tsk1s:tsk3s 应该嵌套在:tsk2s

相应地更改您的表单,如下所示

  <%= f.fields_for :tsk1s do |tsk1| %>

  <ul>
  <li>
  <%= tsk1.number_field :raiz, :placeholder => "Identificador" %>
  <%= tsk1.text_field :descr, :placeholder => "Descrição"  %>
  <%= tsk1.number_field :hour , :placeholder => "Carga Horária" %>
  <%= tsk1.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %>
  <%= f.link_to_add "+Ramo", :tsk2s %>
  <%= tsk1.link_to_remove "-" %> </li>

  <%= tsk1.fields_for :tsk2s do |tsk2| %>

  <ul class="step">
  <li>
  <%= tsk2.number_field :raiz, :placeholder => "Identificador" %>
  <%= tsk2.text_field :descr, :placeholder => "Descrição" %>
  <%= tsk2.number_field :hour, :placeholder => "Carga Horária" %>
  <%= tsk2.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %>
  <%= tsk2.link_to_remove "-" %>  
  <%= f.link_to_add "+Galho", :tsk3s %> </li>

  <%= tsk2.fields_for :tsk3s do |tsk3| %>

  <ul>
  <li>  

  <%= tsk3.number_field :raiz, :placeholder => "Identificador" %>
  <%= tsk3.text_field :descr, :placeholder => "Descrição" %>
  <%= tsk3.number_field :hour, :placeholder => "Carga Horária" %>
  <%= tsk3.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %>
  <%= tsk3.link_to_remove "-" %> </li>
  </ul>
  </ul>
  </ul>
  <% end %>
  <% end %>
  <% end &>
  </ul>
  </ul>
  <br>
  <%= f.link_to_add "+Raiz", :tsk1s %> <br> <br> <br>
  <div class="actions">
  <%= f.submit %>
  </div>
  <% end %>

【讨论】:

  • 收到此错误,未经许可的参数:1436559009615, new_tsk1s "task"=>{"projeto_id"=>"1", "raiz"=>"1", "descr"=>"asd" , "hour"=>"33", "typo"=>"Analitica", "tsk1s_attributes"=>{"1436559009615"=>{"raiz"=>"2", "descr"=>"gf", "小时"=>"5", "typo"=>"Analitica", "_destroy"=>"false"}, "new_tsk1s"=>{"tsk2s_attributes"=>{"1436559010833"=>{"raiz"=> "3", "descr"=>"h", "hour"=>"5", "typo"=>"Analitica", "_destroy"=>"false"}, "new_tsk2s"=>{"tsk3s_attributes" =>{"1436559011808"=>{"raiz"=>"4", "descr"=>"b", "hour"=>"5", "typo"=>"Analitica", "_destroy"=> "false"}}}}}}}, "commit"=>"创建任务"}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
相关资源
最近更新 更多