RoR:嵌套表单模型属性在其他模型属性中

时间:2015-07-10 18:41:03

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

我正在制作一个项目,其中“Project has_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仅限于belongs_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 %>

这实际上只保存任务和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

1 个答案:

答案 0 :(得分:0)

其中一个原因可能是您的表单代码:tsk2s:tsk3s嵌套在 任务 中,但根据模型代码, :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 %>