- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的编程团队像Michael Hartl的Rails教程一样创建了一个帐户激活功能。现在,经过几周的编码,该功能停止了工作。这就是全部功能:
class AccountActivationsController < ApplicationController
#Dieser Controller ist notwendig, um einen neu registrierten Account via Aktivierungsmail zu aktivieren.
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
log_in user
flash[:success] = "Account aktiviert!"
redirect_to user
else
flash[:danger] = "Ungültiger Aktivierungslink"
redirect_to root_url
end
end
end
user = User.find_by(email: params[:email])
http://localhost:3000/account_activations/hX1eY83-wcs8VqZcPa0H=
3g/edit?email=3Dsami.khedira%40stud.uni-hannover.de
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Bitte öffnen Sie Ihr E-Mail Postfach, um den Account zu aktivieren."
redirect_to root_url
else
render 'new'
end
end
class User < ApplicationRecord
# Activates an account.
def activate
update_columns(activated: true, activated_at: Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_columns(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Returns true if a password reset has expired.
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
private
# Converts email to all lower-case.
def downcase_email
self.email = email.downcase
end
# Creates and assigns the activation token and digest.
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
<h1>Bachelorarbeitszuordnung</h1>
<p>Sehr geehrter Herr / Sehr geehrte Frau <%= @user.name %>,</p>
<p>
Sie haben sich für die Bachelorarbeit registriert! Klicken Sie auf den untenstehenden Link, um ihren Account zu aktivieren:
</p>
<%= link_to "Aktivieren", edit_account_activation_url(@user.activation_token,
email: @user.email) %>
class UserMailer < ApplicationMailer
def account_activation(user)
@user = user
mail to: user.email, subject: "Account activation"
end
def password_reset(user)
@user = user
mail to: user.email, subject: "Password reset"
end
end
class ApplicationMailer < ActionMailer::Base
default from: "noreply@stud.uni-hannover.de"
layout 'mailer'
end
Rails.application.routes.draw do
resources :deadlines
resources :preferences
resources :institutes
resources :users
resources :admin, to: 'users#admin'
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get '/home', to: 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/matching', to: 'static_pages#matching'
get '/cockpit', to: 'static_pages#cockpit'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/performance_show', to: 'users#performance_show'
get '/performance_update', to: 'users#performance_update'
post 'preferences/create_all', to: 'preferences#create_all'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
# Routes für die Buttons der GAMS Berechnung
post 'read_matching', to: 'static_pages#read_matching'
post 'delete_matching', to: 'static_pages#delete_matching'
post 'optimize', to: 'static_pages#optimize'
# Route für Button zum Löschen aller Studenten
delete 'delete_all', to: 'users#delete_all'
#Route für Page zum Löschen des eigenen Accounts.
get 'delete_account', to: 'users#delete_account'
delete 'delete_account_sure', to: 'users#delete_account_sure'
end
Started POST "/users" for 127.0.0.1 at 2018-03-13 17:41:09 +0100
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hbZ8A9CWC60nyXYd3nf6Dv0M+d/ViCp0PJ8AmG/fI5ZvyE+hFBt5n8W54gg9yNqZQTfSuOa8PyUD16a3qoRGsg==", "user"=>{"name"=>"Sami Khedira", "mat_number"=>"1234567", "email"=>"sami.khedira@stud.uni-hannover.de", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Erstelle meinen Account"}
[1m[35m (1.0ms)[0m [1m[36mbegin transaction[0m
[1m[36mUser Exists (3.0ms)[0m [1m[34mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ?[0m [["email", "sami.khedira@stud.uni-hannover.de"], ["LIMIT", 1]]
[1m[35mSQL (0.8ms)[0m [1m[32mINSERT INTO "users" ("name", "email", "created_at", "updated_at", "password_digest", "activation_digest", "mat_number") VALUES (?, ?, ?, ?, ?, ?, ?)[0m [["name", "Sami Khedira"], ["email", "sami.khedira@stud.uni-hannover.de"], ["created_at", "2018-03-13 16:41:10.395281"], ["updated_at", "2018-03-13 16:41:10.395281"], ["password_digest", "$2a$10$2BLl1RzF2SOPB9/S5y.oC.W3vUY64GX9jOtd9EfOhrhs3Wnd7Z0Ky"], ["activation_digest", "$2a$10$MaFRiFb195HRm8AQ14OU.ey5ds5qDvR9nznFLBgClNXM21VqQh0AK"], ["mat_number", 1234567]]
[1m[35m (11.9ms)[0m [1m[36mcommit transaction[0m
Rendering user_mailer/account_activation.html.erb within layouts/mailer
Rendered user_mailer/account_activation.html.erb within layouts/mailer (29.8ms)
Rendering user_mailer/account_activation.text.erb within layouts/mailer
Rendered user_mailer/account_activation.text.erb within layouts/mailer (7.8ms)
UserMailer#account_activation: processed outbound mail in 80.2ms
Sent mail to sami.khedira@stud.uni-hannover.de (31.7ms)
Date: Tue, 13 Mar 2018 17:41:11 +0100
From: noreply@stud.uni-hannover.de
To: sami.khedira@stud.uni-hannover.de
Message-ID: <5aa7ff27a2570_3afe126dc7462164@studi-VirtualBox.mail>
Subject: Account activation
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5aa7ff279cbd7_3afe126dc74620d2";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5aa7ff279cbd7_3afe126dc74620d2
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Sehr geehrter Herr Sami Khedira,
Sie haben sich f=C3=BCr die Bachelorarbeit registriert! Klicken Sie auf d=
en untenstehenden Link, um ihren Account zu aktivieren:
<a href=3D"http://localhost:3000/account_activations/hX1eY83-wcs8VqZcPa0H=
3g/edit?email=3Dsami.khedira%40stud.uni-hannover.de">Aktivieren</a>
----==_mimepart_5aa7ff279cbd7_3afe126dc74620d2
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf=
-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<h1>Bachelorarbeitszuordnung</h1>
<p>Sehr geehrter Herr Sami Khedira,</p>
<p>
Sie haben sich f=C3=BCr die Bachelorarbeit registriert! Klicken Sie auf d=
en untenstehenden Link, um ihren Account zu aktivieren:
</p>
<a href=3D"http://localhost:3000/account_activations/hX1eY83-wcs8VqZcPa0H=
3g/edit?email=3Dsami.khedira%40stud.uni-hannover.de">Aktivieren</a>
</body>
</html>
----==_mimepart_5aa7ff279cbd7_3afe126dc74620d2--
Redirected to http://localhost:3000/
Completed 302 Found in 1972ms (ActiveRecord: 65.1ms)
Started GET "/" for 127.0.0.1 at 2018-03-13 17:41:11 +0100
Processing by StaticPagesController#home as HTML
Rendering static_pages/home.html.erb within layouts/application
Rendered static_pages/home.html.erb within layouts/application (22.8ms)
Rendered layouts/_rails_default.html.erb (604.6ms)
Rendered layouts/_shim.html.erb (0.4ms)
Rendered layouts/_header.html.erb (7.5ms)
Rendered layouts/_footer.html.erb (3.8ms)
Completed 200 OK in 765ms (Views: 755.8ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2018-03-13 17:47:23 +0100
Processing by StaticPagesController#home as HTML
Rendering static_pages/home.html.erb within layouts/application
Rendered static_pages/home.html.erb within layouts/application (965.2ms)
Rendered layouts/_rails_default.html.erb (11813.6ms)
Rendered layouts/_shim.html.erb (66.2ms)
Rendered layouts/_header.html.erb (194.1ms)
Rendered layouts/_footer.html.erb (53.3ms)
Completed 200 OK in 14350ms (Views: 14125.1ms | ActiveRecord: 0.0ms)
Started POST "/users" for 127.0.0.1 at 2018-03-14 02:07:04 +0100
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6vTYSvvGONUEC7hEP/C/AqEHAeQ+oQToqHQGKuBsM7ozw/N+w11mWnIb1x4Io5CWU/eYpYwncObyWWC+zKY4Jg==", "user"=>{"name"=>"Sami Khedira", "mat_number"=>"12345678", "email"=>"samikhedira@stud.uni-hannover.de", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Erstelle meinen Account"}
(0.1ms) begin transaction
User Exists (1.9ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ? [["email", "samikhedira@stud.uni-hannover.de"], ["LIMIT", 1]]
SQL (14.4ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at", "password_digest", "activation_digest", "mat_number") VALUES (?, ?, ?, ?, ?, ?, ?) [["name", "Sami Khedira"], ["email", "samikhedira@stud.uni-hannover.de"], ["created_at", "2018-03-14 01:07:04.901137"], ["updated_at", "2018-03-14 01:07:04.901137"], ["password_digest", "$2a$10$whjEx3oPnLxyNFZomWst4uCAPqweV0jBtN342mlx.sJwAm6A4JD7a"], ["activation_digest", "$2a$10$EG9MOsxQYUJC2//VGj6Iyu1CB7/39NP3mMpv1BjE2QrYx2WeZCM7K"], ["mat_number", 12345678]]
(62.6ms) commit transaction
Rendering user_mailer/account_activation.html.erb within layouts/mailer
Rendered user_mailer/account_activation.html.erb within layouts/mailer (9.2ms)
Rendering user_mailer/account_activation.text.erb within layouts/mailer
Rendered user_mailer/account_activation.text.erb within layouts/mailer (9.3ms)
UserMailer#account_activation: processed outbound mail in 142.7ms
Sent mail to samikhedira@stud.uni-hannover.de (116.4ms)
Date: Wed, 14 Mar 2018 02:07:05 +0100
From: noreply@stud.uni-hannover.de
To: samikhedira@stud.uni-hannover.de
Message-ID: <5aa875b964bd3_4c5f1f7f0d42644@studi-VirtualBox.mail>
Subject: Account activation
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5aa875b94bfdf_4c5f1f7f0d4257f";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5aa875b94bfdf_4c5f1f7f0d4257f
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Sehr geehrter Herr Sami Khedira,
Sie haben sich f=C3=BCr die Bachelorarbeit registriert! Klicken Sie auf d=
en untenstehenden Link, um ihren Account zu aktivieren:
<a href=3D"http://localhost:3000/account_activations/WFJAfA0Ed4h-eL13PApg=
ng/edit?email=3Dsamikhedira%40stud.uni-hannover.de">Aktivieren</a>
----==_mimepart_5aa875b94bfdf_4c5f1f7f0d4257f
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf=
-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<h1>Bachelorarbeitszuordnung</h1>
<p>Sehr geehrter Herr / Sehr geehrte Frau Sami Khedira,</p>
<p>
Sie haben sich f=C3=BCr die Bachelorarbeit registriert! Klicken Sie auf d=
en untenstehenden Link, um ihren Account zu aktivieren:
</p>
<a href=3D"http://localhost:3000/account_activations/WFJAfA0Ed4h-eL13PApg=
ng/edit?email=3Dsamikhedira%40stud.uni-hannover.de">Aktivieren</a>
</body>
</html>
----==_mimepart_5aa875b94bfdf_4c5f1f7f0d4257f--
Redirected to http://localhost:3000/
Completed 302 Found in 1177ms (ActiveRecord: 128.8ms)
Started GET "/" for 127.0.0.1 at 2018-03-14 02:07:05 +0100
Processing by StaticPagesController#home as HTML
Rendering static_pages/home.html.erb within layouts/application
Rendered static_pages/home.html.erb within layouts/application (6.4ms)
Rendered layouts/_rails_default.html.erb (234.4ms)
Rendered layouts/_shim.html.erb (0.6ms)
Rendered layouts/_header.html.erb (12.3ms)
Rendered layouts/_footer.html.erb (1.9ms)
Completed 200 OK in 305ms (Views: 301.8ms | ActiveRecord: 0.0ms)
Started GET "/account_activations/WFJAfA0Ed4h-eL13PApg=ng/edit?email=3Dsamikhedira%40stud.uni-hannover.de" for 127.0.0.1 at 2018-03-14 02:07:44 +0100
Processing by AccountActivationsController#edit as HTML
Parameters: {"email"=>"3Dsamikhedira@stud.uni-hannover.de", "id"=>"WFJAfA0Ed4h-eL13PApg=ng"}
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/account_activations_controller.rb:6:in `edit'
最佳答案
您可以像下面这样转换电子邮件并使其网址安全。只要您要验证数据库中所有电子邮件的唯一性,就应该没事。希望这可以帮助。
Base64.urlsafe_encode64("uglas@stud.uni-hannover.de")
# "dWdsYXNAc3R1ZC51bmktaGFubm92ZXIuZGU="
Base64.urlsafe_decode64("dWdsYXNAc3R1ZC51bmktaGFubm92ZXIuZGU=")
# "uglas@stud.uni-hannover.de"
http://localhost:3000/account_activations/9rxXuiQEEXmeOnqcS_m-=
VQ/edit?email="dWdsYXNAc3R1ZC51bmktaGFubm92ZXIuZGU="
params[:email]
时,您可以像这样将其转换回来:
user = User.find_by(Base64.urlsafe_decode64(params[:email]))
关于ruby-on-rails - find_by函数即使成功也不会成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49259530/
目前我正在开发一个 gem,它会覆盖 ActiveRecords where。通过研究,我偶然发现了两种不同的 find_by 实现。一个在 core它使用某种缓存,而来自 FinderMethods
我在 Rails 应用程序中设置了一个命名范围,用于通过 ID(直接来自索引 View )或 UUID(来自电子邮件 - 基本上只是这样用户无法输入)来定位记录任意ID并查看记录) scope :by
我正在使用 ahoy gem 进行分析。在 ahoy_events 表中,我有 json 数据类型的 properties 列。我想根据该列查找特定数据。 假设我有 {"tag":"a","class
我需要从 2 个参数中找到一条记录,但我需要其中一个不区分大小写。当前区分大小写的行是 c = Course.find_by(subject_area: area, cat_number: cat
这些天我正在研究 Ruby on Rails 以提高自己作为开发人员的水平,因此我开始重写我的一个 php 网站以使用该框架。 该网站基本上是一个源代码聚合器,因此我有一个语言模型和一个源模型: cl
假设@news_writers 是一个记录数组。然后我想使用@news_writers 查找由@news_writers 中包含的所有新闻作者撰写的所有新闻项目。 所以我想要这样的东西(但这在语法上是
我有一个奇怪的问题。 我有两个模型用户和角色,它们之间存在多对多关系。我想在用户的角色集合中过滤名称为“管理员”的角色。 在模型中这段代码 puts self.roles.to_s 打印到屏幕: [角
我有获取两个查找结果的代码: results1 = firstModel.find_by_id(id1) results2 = secondModel.find_by_id(id2) allResul
我想找到一个 Post 模型,其 title 是“foo”,但是,如果该帖子不存在,则为“bar”,然后是“foobar”。 为此我写了这个: Post.find_by(title: %w(foo b
MongoID docs似乎很清楚我应该能够运行它并让它工作: Band.find_by(name: "Photek") 但至少对于 MongoID 2.4.11,这给了我一个 NoMethodErr
我正在学习 6.3 版的 Michael Hartel 的 Rails 教程,需要 user_spec 模型的替代代码。他的代码是: let(:found_user) { User.find_by(e
它可能与问题无关,但对于上下文,我在我的应用程序中使用了 devise_invitable gem。我已经开始修改invitations_controller.rb,因为我想保存invited_use
我有一个用户模型,其中包含身份验证方法。 如果我在 Rails 控制台中使用模型进行测试,可以很好地创建用户,然后我可以在电子邮件中进行查找并像这样完美地返回用户。 user = User.find_
我是 Rails 新手。据我所知,查找记录的方法有很多种: find_by_() find(:first, :conditions => { => } where( => ).first 看起来它
假设我有模型(帐户),其中包含以下列 account_id、user_id、party_id、日期和名称 然后我执行: Account.find_by_account_id_and_user_id_a
假设我有模型(帐户),其中包含以下列 account_id、user_id、party_id、日期和名称 然后我执行: Account.find_by_account_id_and_user_id_a
ActiveRecord 的 find_by 方法似乎没有注意到自定义 getter。也就是说,find_by 似乎查看的是实际存储在数据库中的值,而不是 getter 返回的内容。示例: Activ
是否可以对 2 个以上的字段使用 find_by? 喜欢: feedback = Feedback.find_by_field_a_and_field_b_and_field_c("A", "B",
异常处理不适用于 rails 中的 find_by 查询 这里的代码: ab = User.find_by_uniq_token(params[:uniq_token]) 不引发异常...同时:
所以我知道 Rails 在像这样使用时有助于防止 sql 注入(inject): Object.find(:first, :conditions=>["name=?",name]) 但是,我似乎无法确
我是一名优秀的程序员,十分优秀!