This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding multi-model editing using accepts_nested_attributes_for
- Loading branch information
Showing
7 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
# Methods added to this helper will be available to all templates in the application. | ||
module ApplicationHelper | ||
def add_task_link(name, f) | ||
fields = new_child_fields(f, :tasks) | ||
link_to_function(name, "insert_fields('tasks', '#{escape_javascript(fields)}')") | ||
end | ||
|
||
def new_child_fields(form_builder, method, options = {}) | ||
options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new | ||
options[:partial] ||= method.to_s.singularize | ||
options[:form_builder_local] ||= :f | ||
form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f| | ||
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f }) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
class Project < ActiveRecord::Base | ||
has_many :tasks | ||
validates_presence_of :name | ||
accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
<%= error_messages_for :project %> | ||
<% form_for @project do |f| %> | ||
<%= f.error_messages %> | ||
<p> | ||
<%= f.label :name %><br /> | ||
<%= f.text_field :name %> | ||
</p> | ||
|
||
<h3>Tasks</h3> | ||
<div id="tasks"> | ||
<% f.fields_for :tasks do |task_form| %> | ||
<%= render :partial => 'task', :locals => { :f => task_form } %> | ||
<% end %> | ||
</div> | ||
<p><%= add_task_link "New Task", f %></p> | ||
<p><%= f.submit "Submit" %></p> | ||
<% end %> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div class="field"> | ||
<%= f.label :name, "Task" %> | ||
<%= f.text_field :name %> | ||
<%= f.check_box :_delete %> | ||
<%= f.label :_delete, "(remove)" %> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
// Place your application-specific JavaScript functions and classes here | ||
// This file is automatically included by javascript_include_tag :defaults | ||
function insert_fields(element_id, content) { | ||
var new_id = new Date().getTime(); | ||
$(element_id).insert({ | ||
bottom: content.replace(/NEW_RECORD/g, new_id) | ||
}); | ||
} |
This file contains 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