now the adding of the user credential inline is working

Umar M. Sheikh 11 lat temu
rodzic
commit
717a5a6ac0

+ 8 - 1
app/assets/javascripts/users.js

@@ -1,6 +1,13 @@
1 1
 //alert("i get included");
2 2
 function remove_fields(link){
3 3
   $(link).prev().val("1");
4
-  alert($(link).prev().val())
5 4
   $(link).parent(".fields").hide();
6 5
 }
6
+
7
+function add_fields(link, association, content) {
8
+  var new_id = new Date().getTime();
9
+  var regexp = new RegExp("new_" + association, "g")
10
+  $(link).parent().before(content.replace(regexp, new_id));
11
+}
12
+
13
+

+ 3 - 2
app/helpers/application_helper.rb

@@ -20,11 +20,12 @@ module ApplicationHelper
20 20
   end
21 21
 
22 22
   def link_to_add_fields(name, f, options = {})
23
+    association = options[:association]   
23 24
     new_object = f.object.class.reflect_on_association(association).klass.new
24
-    fields = f.fields_for(association, new_object, :child_index => "new_#{ association }") do |builder|
25
+    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
25 26
       render(association.to_s.singularize + "_fields", :f => builder)
26 27
     end
27
-    link_to_function(name, "add_fields(this, \"#{ association }\", \"#{ escape_javascript(fields) }\")", options)
28
+    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
28 29
   end
29 30
   
30 31
 end

+ 1 - 1
app/models/user.rb

@@ -23,7 +23,7 @@ class User < ActiveRecord::Base
23 23
   validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
24 24
 
25 25
   has_many :user_credentials, :dependent => :destroy
26
-  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs.all? { |key, value| value.blank? } },
26
+  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs[:name].blank? },
27 27
                                 :allow_destroy => true
28 28
   attr_accessible :user_credentials_attributes
29 29
   has_many :events, :order => "events.created_at desc", :dependent => :delete_all, :inverse_of => :user

+ 2 - 1
app/views/devise/registrations/_user_credentials_fields.html.erb

@@ -4,5 +4,6 @@
4 4
   <%= f.label :credential_value, "Value" %>
5 5
   <%= f.text_field :credential_value %>
6 6
   <%= f.hidden_field :_destroy %>
7
-  <%= link_to_function "remove", "remove_fields(this)" %>
7
+  <%= link_to_remove_fields("remove", f) %>
8 8
 </p>
9
+

+ 2 - 1
app/views/devise/registrations/edit.html.erb

@@ -52,9 +52,10 @@ hello doctor
52 52
           now the new stuff
53 53
           <div class="control-group">
54 54
             <%= f.fields_for(:user_credentials) do |uc| %>
55
-              <%= render 'user_credentials_fields', :f => uc %>
55
+              <%= render 'user_credential_fields', :f => uc %>
56 56
             <% end %>
57 57
           </div>
58
+          <p><%= link_to_add_fields "Add A Credential", f, :association => :user_credentials %></p>
58 59
         <% end %>
59 60
 
60 61
         <h3>Cancel my account</h3>