瀏覽代碼

now the adding of the user credential inline is working

Umar M. Sheikh 11 年之前
父節點
當前提交
717a5a6ac0

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

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

+ 3 - 2
app/helpers/application_helper.rb

@@ -20,11 +20,12 @@ module ApplicationHelper
   end
 
   def link_to_add_fields(name, f, options = {})
+    association = options[: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|
+    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, "add_fields(this, \"#{ association }\", \"#{ escape_javascript(fields) }\")", options)
+    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
   end
   
 end

+ 1 - 1
app/models/user.rb

@@ -23,7 +23,7 @@ class User < ActiveRecord::Base
   validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
 
   has_many :user_credentials, :dependent => :destroy
-  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs.all? { |key, value| value.blank? } },
+  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs[:name].blank? },
                                 :allow_destroy => true
   attr_accessible :user_credentials_attributes
   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 → app/views/devise/registrations/_user_credential_fields.html.erb

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

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

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