Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%= form_with(model: current_user, local: true, html: {multipart: true}) do |form| %>
- <div class="field">
- <%= form.label :profile_pic %>
- <%= form.file_field :profile_pic, id: :profile_pic %>
- </div>
- <div class="actions">
- <%= form.submit %>
- </div>
- <% end %>
- def update
- @user = current_user
- respond_to do |format|
- if @user.update(user_params)
- format.html { redirect_to @user, notice: 'User was successfully updated.' }
- format.json { render :show, status: :ok, location: @user }
- else
- render action: :edit
- end
- end
- end
- respond_to do |format|
- require 'digest'
- class User < ApplicationRecord
- mount_uploader :profile_pic, ProfilePicUploader
- attr_accessor :password
- before_save :encrypt_new_password
- after_create :build_profile
- has_one :profile
- has_many :trips
- has_many :comments, through: :trips, source: :comments
- has_many :posts, through: :trips, source: :posts
- scope :recent_comments, ->{where("comments.created_at > ? AND user_id = ?", [6.months.ago, self.id]).limit(3)}
- #friends
- has_many :users
- validates :email, uniqueness: {case_sensitive: false, message: 'El correo debe ser Ășnico'}, length: {in: 6..50, too_short: "debe tener al menos %{count} caracteres"}, format: {multiline: true,with: /^.+@.+$/, message: "formato de correo no valido"}
- validates :password, confirmation: true, length: {within: 4..20}, presence: {if: :password_required?}
- validates :password_confirmation, presence: true
- def self.authenticate(email,password)
- user = find_by_email(email)
- return user if user && user.authenticated?(password)
- end
- def authenticated?(password)
- self.hashed_password == encrypt(password)
- end
- protected
- def encrypt_new_password
- return if password.blank?
- self.hashed_password = encrypt(password)
- end
- def password_required?
- hashed_password.blank? || password.present?
- end
- def encrypt(string)
- Digest::SHA1.hexdigest(string)
- end
- def build_profile
- Profile.create(user: self, name: self.name, bio:"Im using Tripper!")
- end
- end
Add Comment
Please, Sign In to add comment