diff --git a/.gitignore b/.gitignore index 429d6ce..1575996 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ build-iPhoneSimulator/ # JetBrain的忽略文件 .idea/* public/data/* +public/userpics/* # rails的忽略文件 @@ -102,3 +103,5 @@ bower.json db/*.sqlite3 + + diff --git a/Gemfile b/Gemfile index d6ad9fb..f900e97 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ gem 'jbuilder', '~> 2.5' gem 'bcrypt', '~> 3.1.7' # will_paginate gem 'will_paginate', '~> 3.1.0' +gem 'sendgrid-ruby' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index eb295f9..182a596 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,6 +118,7 @@ GEM rb-fsevent (0.9.8) rb-inotify (0.9.7) ffi (>= 0.5.0) + ruby_http_client (3.0.0) sass (3.4.22) sass-rails (5.0.6) railties (>= 4.0.0, < 6) @@ -125,6 +126,8 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sendgrid-ruby (4.0.6) + ruby_http_client (~> 3.0.0) spring (2.0.0) activesupport (>= 4.2) spring-watcher-listen (2.0.1) @@ -177,6 +180,7 @@ DEPENDENCIES rails (~> 5.0.0, >= 5.0.0.1) rake sass-rails (~> 5.0) + sendgrid-ruby spring spring-watcher-listen (~> 2.0.0) sqlite3 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2daa33f..def210a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,8 @@ class UsersController < ApplicationController + + before_action :logged_in_user,except: [:new,:create,:forgetpasswd,:changepasswd] + @@user_pics_dir = "public/userpics" + def new @user = User.new end @@ -20,12 +24,6 @@ class UsersController < ApplicationController def update - if @user.update_attributes(user_params) - flash[:success]="profile updated" - redirect_to @user - else - render 'edit' - end end @@ -72,7 +70,18 @@ class UsersController < ApplicationController end def changeprofile - render html: "等待完成" + @user = current_user + result, user, err_string = check_change_profile_params(params[:user]) + if result == false + flash[:danger] = err_string + else + pic_url = save_user_pic params[:user][:pic] + if pic_url.nil? == false + user[:picurl] = pic_url + end + @user.update_attributes(user) + end + redirect_to profile_path end private @@ -82,6 +91,35 @@ class UsersController < ApplicationController :password_confirmation) end + def check_change_profile_params user_params + if user_params.nil? + return false, nil, "修改参数为空" + end + if user_params[:username].length <= 0 + return false, nil, "姓名错误" + end + user = Hash.new + user[:name] = user_params[:username] + user[:profession] = user_params[:profession] + user[:sexual] = user_params[:sexual] + return true, user, nil + end + + def save_user_pic pic + if pic.nil? + return nil + end + if !(File.directory? @@user_pics_dir) + FileUtils.mkdir_p(@@user_pics_dir) + end + ext = File.extname(pic.original_filename) + timestamp = Time.now.to_s + picname = Digest::MD5::hexdigest(pic.original_filename + timestamp) + ext + File.open(File.join(@@user_pics_dir, picname), 'wb') { |f| f.write(pic.read) } + save_name = File.join("userpics", picname) + return save_name + end + def send_code user verify_code = rand(999999).to_s result = UserMailer.send_verify_code(user.email, verify_code).deliver_now diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index e884bad..7b2b73b 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -3,4 +3,9 @@ module UsersHelper user = User.find_by(id: user_id) return user.name if !user.nil? end + + def get_user_sexual user + user.sexual + end end + diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 0937a52..e0ce585 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: "ucasguoren@163.com" + default from: "ucasguoren@heroku.com" layout 'mailer' end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index afb5a78..5c5e81e 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,5 +1,5 @@ class UserMailer < ApplicationMailer - default from: "ucasguoren@163.com" + default from: "ucasguoren@heroku.com" def send_verify_code mail_address, code @verify_code = code diff --git a/app/models/user.rb b/app/models/user.rb index 65885c8..241c5f1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,10 @@ class User < ApplicationRecord before_save :default_values - before_save :default_values def default_values - self.picurl = 'images/avatars/default/avatar.png' + if self.picurl.nil? + self.picurl = 'images/avatars/default/avatar.png' + end end has_many :send_messages, class_name: 'Message', foreign_key: 'send_user', dependent: :destroy diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 4df86d6..357e133 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -6,7 +6,7 @@ <%= render 'layouts/nav' %> - <%= form_tag("/changeprofile", method: "post", class: "form-horizontal", id: "profile") do %> + <%= form_tag("/changeprofile", method: "post", class: "form-horizontal", id: "profile", enctype: "multipart/form-data") do %>