Created
January 29, 2010 09:36
-
-
Save szimek/289599 to your computer and use it in GitHub Desktop.
Nested form helper based on http://transfs.com/devblog/2009/06/26/nested-forms-with-rails-2-3-helpers-and-javascript-tricks, but with Haml and jQuery
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
-# Don't display new assets on form redisplay | |
- unless (form.object.new_record? && form.object.data.dirty?) | |
%li | |
= form.object.name if form.object.data? | |
= form.file_field :data if form.object.new_record? | |
= form.text_field :title | |
= remove_link(form, t('assets.actions.remove')).html_safe! |
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
- semantic_form_for @question, :html => { :multipart => true } do |f| | |
= f.input :body | |
- f.inputs :name => t("models.assets"), :id => "assets" do | |
- f.semantic_fields_for :assets do |asset_form| | |
= f.error_message_on :assets | |
= render :partial => "assets/asset", :locals => { :form => asset_form } | |
%li= link_to_new_nested_form t("assets.actions.add"), f, :assets, :partial => "assets/asset" | |
- f.buttons do | |
= f.commit_button |
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
// Nested forms remove link handler | |
$('.remove').live('click', function() { | |
var link = $(this); | |
if(link.hasClass('remove_new')) { | |
link.parents('li').remove(); | |
} else { | |
link.prev('input[type=hidden]').val("1"); | |
link.parents('li').hide(); | |
} | |
}); |
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
module NestedFormsHelper | |
def remove_link(fields, name="remove") | |
out = '' | |
html_class = 'remove ' | |
if fields.object.new_record? | |
html_class += "remove_new" | |
else | |
html_class += "remove_existing" | |
out << fields.hidden_field(:_delete) | |
end | |
out << link_to(name, "##{fields.object.class.name.underscore}", :class => html_class) | |
out | |
end | |
def generate_html(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] ||= :form | |
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 | |
def link_to_new_nested_form(name, 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] ||= :form | |
options[:element_id] ||= method.to_s | |
link_to_function name do |page| | |
html = generate_html( | |
form_builder, | |
method, | |
:object => options[:object], | |
:partial => options[:partial], | |
:form_builder_local => options[:form_builder_local] | |
) | |
page << %{ | |
$('##{options[:element_id]} ol li:last-child').before("#{ escape_javascript html }".replace(/NEW_RECORD/g, new Date().getTime())); | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment