Created
May 2, 2016 11:57
-
-
Save samflores/a72ae5f1da0cd1eb4cc22993b5fc9622 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<tr class="nested-fields"> | |
<td> | |
<%= f.text_field :name %> | |
</td> | |
<td> | |
<%= link_to_remove_association 'Remover tarefa' %> <!-- IMPORTANTE: método adicionado pela gem que cria um link que executa um JS para REMOVER o trecho no form --> | |
</td> | |
</tr> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for @project do |f| %> | |
<h2>Projeto</h2> | |
<div class="field"> | |
<%= f.label :name %> | |
<%= f.text_field :name %> | |
</div> | |
<h3>Tarefas<%h3> | |
<table id="tasks"> | |
<% f.fields_for :tasks do |t| %> | |
<%= render 'task_fields', f: t %> <!-- IMPORTANTE: aponta pro arquivo da partial que contem o trecho do form que será acidionado ao clicar no link --> | |
<% end %> | |
<div class="links"> | |
<%= link_to_add_association 'Nova tarefa', f, :tasks %> <!-- IMPORTANTE: método adicionado pela gem que cria um link que executa um JS para adicionar o trecho no form --> | |
</div> | |
</table> | |
<%= f.submit %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project < ActiveRecord::Base | |
has_many :tasks | |
accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true # IMPORTANTE: diz que o formulário para criação de Project deve aceitar campos para as Tasks associadas a ele | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProjectsController < ApplicationController | |
# ... | |
def project_params # 👇 campos de Task que serão aceitos vindos do fomulário. id e _destroy são necessários. os outros são os campos que você precisar no model | |
params.require(:project).permit(:name, tasks_attributes: [:id, :name, :_destroy]) | |
# 👆 campos de Project que serão aceitos vindos do form | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task < ActiveRecord::Base | |
belongs_to :project | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment