Created
August 14, 2011 02:58
-
-
Save ZachBeta/1144512 to your computer and use it in GitHub Desktop.
nested form error
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
<%= form_for(@task) do |f| %> | |
<% if @task.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2> | |
<ul> | |
<% @task.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
</div> | |
<%= link_to_add_fields "Add timer", f, :clock_ins %> | |
<% f.fields_for :clock_ins do |builder| %> | |
<%= render "clock_in_fields", :f => builder %> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% 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
// Place your application-specific JavaScript functions and classes here | |
// This file is automatically included by javascript_include_tag :defaults | |
function remove_fields (link) { | |
$(link).previous("input[type=hidden]").value = "1"; | |
$(link).up(".fields").hide(); | |
} | |
function add_fields(link, association, content) { | |
var new_id = new Date().getTime(); | |
var regexp = new RegExp("new_" + association, "g") | |
$(link).up().insert({ | |
before: content.replace(regexp, 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
# Methods added to this helper will be available to all templates in the application. | |
module ApplicationHelper | |
def link_to_remove_fields(name, f) | |
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") | |
end | |
def link_to_add_fields(name, f, association) | |
new_object = f.object.class.reflect_on_association(association).klass.new | |
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| | |
render(association.to_s.singularize + "_fields", :f => builder) | |
end | |
link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment