Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.13 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. User edit Profile Form Not Working
  2. <%= form_for @user, :html=> { :multipart => true} do |f| %>
  3.   <%= render 'shared/error_messages', :object => f.object %>
  4.  <div class="field">
  5.   <%= f.label :name %><br />
  6.   <%= f.text_field :name %>
  7.  </div>
  8.  <div class="field">
  9.    <%= f.label :email %><br />
  10.    <%= f.text_field :email %>
  11.  </div>
  12. <div class="field">
  13.   <%= f.label :password %><br />
  14.   <%= f.password_field :password %>
  15. </div>
  16.    <div class="field">
  17.      <%= f.label :password_confirmation, "Confirmation" %><br />
  18.    <%= f.password_field :password_confirmation %>
  19.    </div>
  20.   <div class="actions">
  21.       <%= f.submit "Update" %>
  22.   </div>
  23.  <% end %>
  24.  
  25.   <%= form_for @user, :html=> { :multipart => true} do |f| %>
  26. <%= f.file_field :photo %>
  27.        <br />
  28.      <%= f.submit "Update" %>
  29.   <% end %>
  30.        
  31. class User < ActiveRecord::Base
  32.  
  33.   attr_accessor :password
  34.  
  35.   attr_accessible :name, :email, :password, :password_confirmation, :photo
  36.  
  37.     has_attached_file :photo,
  38.                   :styles => {
  39.                   :thumb=> "50x50#",
  40.                   :small  => "220x220>" },
  41.                   :storage => :s3,
  42.                   :s3_credentials => "#{Rails.root}/config/s3.yml",
  43.                   :path => "/:style/:id/:filename"
  44.  
  45. has_many :microposts, :dependent => :destroy
  46. has_many :relationships, :foreign_key => "follower_id",
  47.                            :dependent => :destroy
  48. has_many :following, :through => :relationships, :source => :followed
  49. has_many :reverse_relationships, :foreign_key => "followed_id",
  50.                                    :class_name => "Relationship",
  51.                                    :dependent => :destroy
  52. has_many :followers, :through => :reverse_relationships, :source => :follower
  53.  
  54.    email_regex = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  55.  
  56.    validates :name,  :presence => true,
  57.                 :length   => { :maximum => 50 }
  58.    validates :email, :presence   => true,
  59.                 :format     => { :with => email_regex },
  60.                 :uniqueness => { :case_sensitive => false }
  61.  
  62.    validates :password, :presence     => true,
  63.                                        :confirmation => true,
  64.                                        :length       => { :within => 6..40 }
  65.  
  66.                                         before_save :encrypt_password
  67.        
  68. validates :password, :presence     => true,
  69.                                        :if => :validate_password?,
  70.                                        :confirmation => true,
  71.                                        :length       => { :within => 6..40 }
  72.  
  73. def validate_password?
  74.   if new_record?
  75.     return true
  76.   else
  77.     if password.to_s.empty?
  78.       return false
  79.     else
  80.       return true
  81.     end
  82.   end
  83. end
  84.        
  85. def encrypt_password
  86.   return if password.to_s.empty?
  87.   ...
  88.   ... existing code
  89.   ...
  90. end
  91.        
  92. validates_length_of       :password, :length => { :within => 6..40 }, :allow_blank => true
  93. validates_confirmation_of :password
  94. validates_presence_of     :password, :on => :create
  95.        
  96. validates :password, :presence     => true,
  97.                                        :on => :create,
  98.                                        :confirmation => true,
  99.                                        :length       => { :within => 6..40 }