ruby on rails - RoR: Nested Form , Hidden Field, value -


i have huge nested form, has task , field_for tsk1, tsk2 , tsk3, them belongs task, i'd tsk2 id of tsk1 , tsk3 of tsk2, how can handle this? have tried hidden fields value => [:tsk1_id] didn't worked.

_form = http://pastebin.com/k8qs9tqw

task controller

class taskscontroller < applicationcontroller   before_action :set_task, only: [:show, :edit, :update, :destroy]    # /tasks   # /tasks.json   def index     @tasks = task.all   end    # /tasks/1   # /tasks/1.json   def show   end    # /tasks/new   def new     @task = task.new     @tasks = task.all   end     # /tasks/1/edit   def edit   end    # post /tasks   # post /tasks.json   def create     @task = task.new(task_params)      respond_to |format|       if @task.save         format.html { redirect_to projetopo_path, notice: 'task 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 |format|       if @task.update(task_params)         format.html { redirect_to @task, notice: 'task 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 |format|       format.html { redirect_to tasks_url, notice: 'task destroyed.' }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_task       @task = task.find(params[:id])     end      # never trust parameters scary internet, allow 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 

task model

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 model

class tsk1 < activerecord::base belongs_to :task has_many :tsk2s, through: :task accepts_nested_attributes_for :tsk2s, allow_destroy: true    end 

tsk2 model

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 model

class tsk3 < activerecord::base belongs_to :tsk2 belongs_to :task  end 

ids not assigned until save records, there's no way form can submit tsk1_id - doesn't exist yet.

what you'll need add wiring in controller attach 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 

part of trouble might strange schema. you're accessing tskns through: :task , others directly, makes wiring complicated.

you might consider removing tsk1, tsk2, , tsk3 in favor of:

  1. just task optional parent_id. can build tree of tasks , dependencies , functionality out of gems acts_as_tree.
  2. task , subtask, order attribute on subtask. can assign subtasks tasks , specify order need finished in.

rails' form helpers handle these cases better current version.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -