RoR:嵌套形式、隐藏字段、值

RoR: Nested Form , Hidden Field, value

我有一个巨大的嵌套表单,其中有一个 TASK 和一个 field_for tsk1、tsk2 和 tsk3,它们都属于 TASK,但我想 tsk2 来获取tsk2 的 tsk1 和 tsk3,我该如何处理?我尝试过使用值 => [:tsk1_id] 的隐藏字段,但没有成功。

_form = http://pastebin.com/K8qS9Tqw

任务控制器

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
    @tasks = Task.all
  end


  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to projetopo_path, 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
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { render :show, status: :ok, location: @task }
      else
        format.html { render :edit }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
  def task_params
  params.require(:task).permit( :projeto_id, :raiz, :descr, :hour, :typo,  :tsk1s_attributes => [ :raiz, :descr, :hour, :typo, :_destroy, :task_id, :tsk2s_attributes => [:tsk1_id]], :tsk2s_attributes => [ :raiz, :descr, :hour, :typo, :task_id, :tsk1_id ,:_destroy, :tsk3s_attributes => [:tsk2_id]], :tsk3s_attributes => [ :raiz, :descr, :hour, :typo, :tsk2_id, :_destroy, :task_id])
end
end

任务模型

class Task < ActiveRecord::Base
has_many :tsk1s
has_many :tsk2s 
has_many :tsk3s
has_many :projetos
belongs_to :projeto

accepts_nested_attributes_for :tsk1s, allow_destroy: true
accepts_nested_attributes_for :tsk2s, allow_destroy: true
accepts_nested_attributes_for :tsk3s, allow_destroy: true

end

tsk1 型号

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



end

tsk2 模型

class Tsk2 < ActiveRecord::Base
belongs_to :tsk1
has_many :tsk3s , through: :tsk1
belongs_to :task

accepts_nested_attributes_for :tsk3s, allow_destroy: true

end

tsk3 模型

class Tsk3 < ActiveRecord::Base
belongs_to :tsk2
belongs_to :task

end

在您 save 记录之前不会分配 ID,因此表单无法提交类似 tsk1_id 的内容 - 它尚不存在。

您需要做的是在控制器中添加接线,将 tsk2 连接到 tsk1 并将 tsk3 连接到 tsk2

@task = Task.new(task_params)
@task.tsk1s.first.tsk2 = @task.tsk2s.first
@task.tsk2s.first.tsk3 = @task.tsk3s.first
@task.save

部分问题可能在于这是一个奇怪的架构。您正在直接访问一些 tskN through: :task 和其他一些,这使得连接变得复杂。

您可以考虑删除 Tsk1Tsk2Tsk3 以支持:

  1. 只是一个 Task 和一个可选的 parent_id。然后你可以构建任何任务和依赖关系树,并从像 acts_as_tree.
  2. 这样的 gem 中获得一些功能
  3. TaskSubtask,在 Subtask 上有一个 order 属性。然后你可以将 Subtasks 分配给 Tasks 并指定它们需要完成的 order

Rails' 表单助手将比您当前的版本更好地处理这些情况。

很难理解你想要什么,因为在你的关系芯片中有歧义引用

class Task < ActiveRecord::Base
  has_many :tsk1s
  has_many :tsk2s, through: tsk1s
  has_many :tsk3s, through: tsk2s
  has_many :projetos
  belongs_to :projeto

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

end
class Tsk2 < ActiveRecord::Base
belongs_to :tsk1
has_many :tsk3s

accepts_nested_attributes_for :tsk3s, allow_destroy: true

end

你必须修改参数以接受上面的修改(我假设你可以解决这个问题)

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

在您看来

   ### inputs for task ####
<%= f.fields_for :tsk1s do |tsk1| %>
   ### inputs for tsk1 ####
 <%= tsk1.fields_for :tsk2s do |tsk2| %>
      ### inputs for tsk2 ####
 <%= tsk2.fields_for :tsk3s do |tsk3| %>
      ### inputs for tsk3 ####