Skip to content

Instantly share code, notes, and snippets.

@mattherick
Last active December 26, 2015 21:19
Show Gist options
  • Save mattherick/7215559 to your computer and use it in GitHub Desktop.
Save mattherick/7215559 to your computer and use it in GitHub Desktop.
# my form:
<%= form_for [refinery, :projects_admin, @project] do |f| -%>
<%= render '/refinery/admin/error_messages', :object => @project, :include_object_name => true %>
<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale %>
<div class='field'>
<%= f.label :name -%>
<%= f.text_field :name, :class => 'larger widest' -%>
</div>
<div class='field'>
<%= f.label :logo -%>
<%= render '/refinery/admin/image_picker',
:f => f,
:field => :logo_id,
:image => @project.logo,
:toggle_image_display => false -%>
</div>
<fieldset>
<legend>Teaser</legend>
<ul id="flat-items">
<%= f.fields_for(:teasers) do |teaser_builder| %>
<%= render "/refinery/projects/admin/projects/teaser_fields", :f => teaser_builder %>
<% end %>
<%= (link_to_add_fields refinery_icon_tag('add.png'), f, :teasers) %>
</ul>
</fieldset>
<%= render '/refinery/admin/form_actions', :f => f,
:continue_editing => false,
:delete_title => t('delete', :scope => 'refinery.projects.admin.projects.project'),
:delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @project.title) -%>
<% end -%>
<% content_for :javascripts do -%>
<script>
$(document).ready(function(){
page_options.init(false, '', '');
});
</script>
<% end -%>
# my link_to_add_fields helper (called in form line 27)
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, :child_index => id) do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to(name, '#', :class => "data-add-fields", :data => { :id => id, :fields => fields.gsub("\n", "")}, :tooltip => "Add new")
end
# my _teaser_fields.html.erb partial (which gets rendered in form line 25)
<li>
<div class="data-item-fields">
<span class="handle">[Sort]</span>
<div class="field">
<%= f.label :url_type %>
<%= f.select :url_type, options_for_select(types_for_select, (f.object.is_external? ? "external" : "internal")), {}, :class => "teaser_url_type" %>
</div>
<div class="field data-page <%= f.object.is_external? ? "display_none" : "" %>">
<%= f.label :page %>
<%= f.select :page_id, all_pages_for_select, { :include_blank => true }, :class => "teaser_page_id" %>
</div>
<div class="field data-url <%= f.object.is_internal? ? "display_none" : "" %>">
<%= f.label :external_url %>
<%= f.text_field :external_url, :value => f.object.external_url.blank? ? "http://www." : f.object.external_url %>
</div>
<%= f.hidden_field :position, :class => "currentposition" %>
<div class="data-destroy-field">
<%= f.hidden_field :_destroy %>
</div>
<div class='field'>
<%= f.label :thumbnail_image -%>
<%= render '/refinery/admin/image_picker',
:f => f,
:field => :image_id,
:image => f.object.image,
:toggle_image_display => false -%>
</div>
<div class="field">
<%= f.label :text %>
<%= f.text_area :text, :rows => 4, :columns => 50 %>
</div>
<%= link_to refinery_icon_tag('delete.png'), "#", :class => "data-remove-fields", :tooltip => "Delete" %>
</div>
</li>
# the relevant js
$("form").on("click", ".data-add-fields", function(event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data("id"), "g");
$(this).before($(this).data("fields").replace(regexp, time));
return event.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment