-
Notifications
You must be signed in to change notification settings - Fork 12
Chapter 8 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Chapter 8 #7
Conversation
ready |
@user = User.find_by id: params[:id] | ||
unless @user | ||
flash[:error] = "User not found." | ||
redirect_to root_path and return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mặc định line cuối cùng trong method là ruby tự return rồi em, ko cần khai báo
redirect_to root_path and return | |
redirect_to root_path |
end | ||
# Returns true if the user is logged in, false otherwise. | ||
def logged_in? | ||
!current_user.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!current_user.nil? | |
current_user.present? |
def birthday_within_last_100years | ||
return unless birthday.present? | ||
if birthday < 100.years.ago.to_date | ||
errors.add(:birthday, "must be within the last 100 years") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n cho mấy cái message trong model này đi e nhé
private | ||
|
||
def birthday_within_last_100years | ||
return unless birthday.present? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nên đưa điều kiện đơn giản vào lúc khai báo validate ở trên nha
validate :birthday_within_last_100years, if: ->{birthday.present?}
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
đừng để line trống nhiều như này e, viết như này model về sau nó sẽ dài dòng không cần thiết
{ prompt: "Select Gender" }, class: "form-control" %> | ||
</div> | ||
|
||
<%= f.submit "Create my account", class: "btn btn-primary" %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n cho hết nhé
@@ -73,3 +78,4 @@ group :test do | |||
gem "selenium-webdriver" | |||
gem "webdrivers" | |||
end | |||
gem "rack-mini-profiler" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lỗi end line nè em, thêm line trống vào cuối file này
user = User.find_by(email: params.dig(:session, :email)&.downcase) | ||
if user && user.authenticate(params.dig(:session, :password)) | ||
log_in user # Đây sẽ thiết lập session[:user_id] = user.id | ||
redirect_to user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thêm flash msg báo login thành công nữa đi e
end | ||
def create | ||
@user = User.new(user_params) | ||
#byebug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ko có để byebug lại nhé, xóa lun e
WHAT
HOW
Sessions
Create login form
sessions/new.html.erb
) with an email/password field.get "/login"
) and link to this form in the navbar if user is not logged in.Authenticate
session[:user_id]
; otherwise, render the form with an error message.Login
logged_in?
is true.Logout
/logout
).session[:user_id]
and redirect to root path.WHY
Evidence
-Successful Logout:
Notes
flash.now
method is useful for displaying error messages on rendered pages.method: :delete
ordata: { turbo_method: :delete }
for the logout link (depending on your JS/Turbo setup).