i'm making project "project has_many tasks, , task has task1, task1 has task 2 , task2 has task3. "
but im stuck in making way insert tasks in 1 form,
im using nested form gem,
this thought'd work.
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 model
class task < activerecord::base has_many :tsk1s belongs_to :projeto accepts_nested_attributes_for :tsk1s, allow_destroy: true end tsk1.erb model
class tsk1 < activerecord::base belongs_to :task has_many :tsk2s accepts_nested_attributes_for :tsk2s, allow_destroy: true end same tsk2.erb, tsk3 belongs_to
a piece of form bellow, adding tsk2, tsk3 same , tsk1 too.
<%= f.fields_for :tsk2s |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 %> this saves task , tsk1 attributes,
task controller:
def create @task = task.new(task_params) respond_to |format| if @task.save format.html { redirect_to @task, 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
def new @task = task.new end
full form code http://pastebin.com/dswzgfns
one of reason in form code, :tsk2s, :tsk3s nested in tasks per model code, :tsk2s should nested in :tsk1s , :tsk3s should nested in :tsk2s.
change form accordingly below
<%= f.fields_for :tsk1s |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 |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 |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 %>
Comments
Post a Comment