RoR:嵌套表格,隐藏字段,价值

时间:2015-07-22 14:08:54

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

我有一个巨大的嵌套表单,它有一个TASK和一个field_for tsk1,tsk2和tsk3,它们都属于TASK,但是我想用tsk2来获取tsk2的tsk1和tsk3的id ,我该怎么处理?我尝试了隐藏的字段值=> [: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

2 个答案:

答案 0 :(得分:0)

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

您需要做的是在控制器中添加接线,以便将tsk2附加到tsk1tsk3附加到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. TaskSubtaskorder上有Subtask个属性。然后,您可以将Subtask指定给Task,并指定他们需要完成的order
  3. Rails的表单帮助程序将比当前版本更好地处理这些情况。

答案 1 :(得分:0)

这是非常复杂的动摇和你想要的东西,因为在你的关系中是模棱两可的参考

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 ####

在你看来

cursor = collection.aggregate(
  [
      {"$match": {"NAME":{"$in":NAMELIST}}},
      {
          "$group":
              {
                  "_id":"$NAME",
                  "average": {"$avg":"$Price"},                                                                              
                  "max": {"$max":"$Price"},
                  "min": {"$min":"$Price"},
                  "count": {"$sum": 1},
             }
     }
  ]
  )