Permalink
Browse files

-

  • Loading branch information...
danbronsema committed Feb 17, 2011
1 parent 9a078e7 commit 0e0fff627d5037d0a5a65892d1c8033d6ce91a98
View
@@ -9,6 +9,7 @@ gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'carrierwave'
gem 'rmagick'
gem "jquery-rails"
+gem "nested_form"
# Use unicorn as the web server
View
@@ -44,6 +44,7 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
+ nested_form (0.0.0)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
@@ -79,6 +80,7 @@ PLATFORMS
DEPENDENCIES
carrierwave
jquery-rails
+ nested_form
rails (= 3.0.3)
rmagick
sqlite3-ruby
@@ -1,7 +1,7 @@
<h1>Assets</h1>
<p>Listing all assets</p>
<% @assets.each do |asset| %>
-<p> <%= link_to image_tag(asset.image.url(:thumb)), asset.image.url, {:class => "fancybox"} %>
+<p> <%= link_to(image_tag(asset.image.url(:thumb)), asset.image.url) %>
<small> <%= asset.image.file.filename %> -
<%= number_to_human_size(asset.image.file.size) %></small>
</p>
@@ -1,4 +1,4 @@
-<%= form_for @user, :html => {:multipart => true} do |f| %>
+<%= nested_form_for @user, :html => {:multipart => true} do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
@@ -20,23 +20,21 @@
<%= f.text_field :bio %>
</div>
- <div class="field">
<%= f.fields_for :assets do |asset| %>
-
+
<% if asset.object.new_record? %>
- <%= asset.file_field(:image) %>
- <% else %>
- <div class="image_field">
- <%= image_tag(asset.object.image.url(:thumb)) %>
- <%= asset.hidden_field :_destroy %>
- <%= link_to_function "remove", "remove_fields(this)" %>
- </div>
+ <%= asset.file_field(:image) %>
+ <% else %>
+ <%= image_tag(asset.object.image.url(:thumb)) %>
+ <%= asset.hidden_field :_destroy %>
+ <%= asset.link_to_remove "remove" %>
<% end %>
-
+
<% end %>
- </div>
-
+
+ <p><%= f.link_to_add "Add image", :assets %></p>
+
<div class="actions">
<%= f.submit %>
</div>
@@ -19,4 +19,4 @@
<%= link_to 'Edit', edit_user_path(@user) %> |
-<%= link_to 'Back', users_path %>
+<%= link_to 'Back', users_path %>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,46 @@
+$(function() {
+$('form a.add_nested_fields').live('click', function() {
+ // Setup
+ var assoc = $(this).attr('data-association'); // Name of child
+ var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
+
+ // Make the context correct by replacing new_<parents> with the generated ID
+ // of each of the parent objects
+ var context = ($(this).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
+
+ // context will be something like this for a brand new form:
+ // project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
+ // or for an edit form:
+ // project[tasks_attributes][0][assignments_attributes][1]
+ if(context) {
+ var parent_names = context.match(/[a-z_]+_attributes/g) || [];
+ var parent_ids = context.match(/[0-9]+/g);
+
+ for(i = 0; i < parent_names.length; i++) {
+ if(parent_ids[i]) {
+ content = content.replace(
+ new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
+ '$1[' + parent_ids[i] + ']'
+ )
+ }
+ }
+ }
+
+ // Make a unique ID for the new child
+ var regexp = new RegExp('new_' + assoc, 'g');
+ var new_id = new Date().getTime();
+ content = content.replace(regexp, new_id);
+
+ $(this).before(content);
+ return false;
+});
+
+$('form a.remove_nested_fields').live('click', function() {
+ var hidden_field = $(this).prev('input[type=hidden]')[0];
+ if(hidden_field) {
+ hidden_field.value = '1';
+ }
+ $(this).closest('.fields').hide('fast');
+ return false;
+});
+});
@@ -54,3 +54,4 @@ div.field, div.actions {
font-size: 12px;
list-style: square;
}
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered

0 comments on commit 0e0fff6

Please sign in to comment.