Skip to content

Commit 0c15edd

Browse files
committed
Chapter 3, 4, 5
1 parent 12182eb commit 0c15edd

27 files changed

+369
-41
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@
3131
/config/master.key
3232
/config/database.yml
3333
/test/*
34+
35+
config/settings.local.yml
36+
config/settings/*.local.yml
37+
config/environments/*.local.yml

.rubocop.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Style/LambdaCall:
282282
- call
283283
- braces
284284

285-
Metrics/LineLength:
285+
Layout/LineLength:
286286
Max: 80
287287
AllowURI: true
288288
Exclude:
@@ -346,10 +346,6 @@ Style/PercentLiteralDelimiters:
346346
'%W': ()
347347
'%x': ()
348348

349-
Naming/PredicateName:
350-
ForbiddenPrefixes:
351-
- get_
352-
353349
Style/RaiseArgs:
354350
EnforcedStyle: exploded
355351
SupportedStyles:

.rubocop_enabled.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Layout/HashAlignment:
1414
one line.
1515
Enabled: true
1616

17-
Layout/Layout/ParameterAlignment:
17+
Layout/ParameterAlignment:
1818
Description: >-
1919
Align the parameters of a method call if they span more
2020
than one line.
@@ -257,7 +257,7 @@ Style/LineEndConcatenation:
257257
line end.
258258
Enabled: true
259259

260-
Metrics/LineLength:
260+
Layout/LineLength:
261261
Description: "Limit lines to 80 characters."
262262
Enabled: true
263263

@@ -353,10 +353,6 @@ Style/PerlBackrefs:
353353
Description: "Avoid Perl-style regex back references."
354354
Enabled: true
355355

356-
Naming/PredicateName:
357-
Description: "Check the names of predicate methods."
358-
Enabled: true
359-
360356
Style/Proc:
361357
Description: "Use proc instead of Proc.new."
362358
Enabled: true
@@ -622,10 +618,6 @@ Layout/EndAlignment:
622618
Description: "Align ends correctly."
623619
Enabled: true
624620

625-
Style/EndBlock:
626-
Description: "END blocks should not be placed inside method definitions."
627-
Enabled: true
628-
629621
Lint/EnsureReturn:
630622
Description: "Never use return in an ensure block."
631623
Enabled: true

Dockerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
FROM ruby:3.2.2
2+
3+
# Set timezone để tránh lỗi timezone data source
4+
ENV TZ=Asia/Ho_Chi_Minh
5+
6+
# Cài đặt các package cần thiết bao gồm tzdata
7+
RUN apt-get update -qq && apt-get install -y \
8+
build-essential \
9+
libpq-dev \
10+
nodejs \
11+
default-mysql-client \
12+
yarn \
13+
tzdata \
14+
dos2unix \
15+
&& apt-get clean \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# Set working directory
19+
WORKDIR /app
20+
21+
# Copy Gemfile trước để tận dụng Docker layer caching
22+
COPY Gemfile Gemfile.lock ./
23+
24+
# Install gems
25+
RUN bundle install
26+
27+
# Copy toàn bộ source code
28+
COPY . .
29+
30+
# Fix line endings cho tất cả các file quan trọng
31+
RUN find . -type f \( -name "*.rb" -o -name "*.yml" -o -name "*.yaml" -o -name "*.sh" \) -exec dos2unix {} \; && \
32+
find ./bin -type f -exec dos2unix {} \; && \
33+
find ./bin -type f -exec chmod +x {} \; && \
34+
find . -name "rails" -type f -exec dos2unix {} \; && \
35+
find . -name "rake" -type f -exec dos2unix {} \; && \
36+
find . -name "bundle" -type f -exec dos2unix {} \;
37+
38+
# Fix quyền thực thi cho bin directory
39+
RUN chmod -R +x ./bin
40+
41+
# Expose port
42+
EXPOSE 3000
43+
44+
# Default command với đường dẫn đầy đủ
45+
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]

Gemfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ gem "tzinfo-data", platforms: %i(mingw mswin x64_mingw jruby)
4343
gem "bootsnap", require: false
4444

4545
# Use Sass to process CSS
46-
# gem "sassc-rails"
46+
gem "sassc-rails"
47+
gem "bootstrap", "~> 5.3.2"
48+
4749

4850
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html]
4951
# gem "image_processing", "~> 1.2"
@@ -73,3 +75,6 @@ group :test do
7375
gem "selenium-webdriver"
7476
gem "webdrivers"
7577
end
78+
79+
# For application configuration
80+
gem 'config'

Gemfile.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ GEM
7373
bindex (0.8.1)
7474
bootsnap (1.16.0)
7575
msgpack (~> 1.2)
76+
bootstrap (5.3.5)
77+
popper_js (>= 2.11.8, < 3)
7678
builder (3.2.4)
7779
capybara (3.39.2)
7880
addressable
@@ -84,12 +86,17 @@ GEM
8486
regexp_parser (>= 1.5, < 3.0)
8587
xpath (~> 3.2)
8688
concurrent-ruby (1.2.2)
89+
config (5.6.1)
90+
deep_merge (~> 1.2, >= 1.2.1)
91+
ostruct
8792
crass (1.0.6)
8893
date (3.3.3)
8994
debug (1.8.0)
9095
irb (>= 1.5.0)
9196
reline (>= 0.3.1)
97+
deep_merge (1.2.2)
9298
erubi (1.12.0)
99+
ffi (1.17.2-x86_64-linux-gnu)
93100
globalid (1.1.0)
94101
activesupport (>= 5.0)
95102
i18n (1.14.1)
@@ -132,10 +139,12 @@ GEM
132139
nio4r (2.5.9)
133140
nokogiri (1.15.4-x86_64-linux)
134141
racc (~> 1.4)
142+
ostruct (0.6.3)
135143
parallel (1.23.0)
136144
parser (3.2.2.3)
137145
ast (~> 2.4.1)
138146
racc
147+
popper_js (2.11.8)
139148
public_suffix (5.0.3)
140149
puma (5.6.6)
141150
nio4r (~> 2.0)
@@ -199,6 +208,14 @@ GEM
199208
rubocop (>= 1.7.0, < 2.0)
200209
ruby-progressbar (1.13.0)
201210
rubyzip (2.3.2)
211+
sassc (2.4.0)
212+
ffi (~> 1.9)
213+
sassc-rails (2.1.2)
214+
railties (>= 4.0.0)
215+
sassc (>= 2.0)
216+
sprockets (> 3.0)
217+
sprockets-rails
218+
tilt
202219
selenium-webdriver (4.10.0)
203220
rexml (~> 3.2, >= 3.2.5)
204221
rubyzip (>= 1.2.2, < 3.0)
@@ -213,6 +230,7 @@ GEM
213230
stimulus-rails (1.2.2)
214231
railties (>= 6.0.0)
215232
thor (1.2.2)
233+
tilt (2.6.1)
216234
timeout (0.4.0)
217235
turbo-rails (1.4.0)
218236
actionpack (>= 6.0.0)
@@ -243,7 +261,9 @@ PLATFORMS
243261

244262
DEPENDENCIES
245263
bootsnap
264+
bootstrap (~> 5.3.2)
246265
capybara
266+
config
247267
debug
248268
importmap-rails
249269
jbuilder
@@ -253,6 +273,7 @@ DEPENDENCIES
253273
rubocop (~> 1.26)
254274
rubocop-checkstyle_formatter
255275
rubocop-rails (~> 2.14.0)
276+
sassc-rails
256277
selenium-webdriver
257278
sprockets-rails
258279
stimulus-rails

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
# Rails tutorial - Sample App
22

33
# 1. Phiên bản cài đặt để chạy được dự án:
4+
45
- Ruby: 3.2.2
56
- Rails: 7.0.7
67
- MySQL
78

89
# 2. Hướng dẫn cài dự án:
10+
911
## 2.1 Cài đặt config(chỉ chạy lần đầu)
12+
1013
- Tạo config database cho dự án: cp config/database.yml.example config/database.yml
1114
- Chạy lệnh để tạo database: rails db:create
1215
- Bật server: rails s
13-
Truy cập đường dẫn http://localhost:3000/ hiển thị Rails là thành công
16+
Truy cập đường dẫn http://localhost:3000/ hiển thị Rails là thành công
17+
18+
1. Research gem config
19+
20+
- gem config là gem dùng để quản lý biến cấu hình trong Rails/Ruby một cách gọn gàng, dễ maintain
21+
- Thay vì hardcode hoặc phụ thuộc ENV, có thể:
22+
- Tạo file config/settings.yml hoặc settings.local.yml để lưu cấu hình.
23+
- Truy cập dễ dàng qua Settings.api_key hoặc Settings[:api_key]
24+
25+
2. Research I18n, I18n lazy lookup
26+
27+
- I18n (Internationalization) trong Rails cho phép ứng dụng hỗ trợ đa ngôn ngữ.
28+
- Các chuỗi văn bản được lưu trong config/locales/\*.yml
29+
- Rails tự động load theo I18n.locale
30+
31+
3. What is Attack CSRF, XSS? and How to prevent them in Rails?
32+
33+
- CSRF : Là tấn công khi attacker giả mạo request của user đã authenticated.
34+
- Ví dụ: attacker gửi form POST từ site khác, user đang login sẽ thực hiện hành động trái phép.
35+
36+
4. XSS (Cross-Site Scripting)
37+
38+
- Là tấn công chèn script độc hại vào trang web, ví dụ:
39+
- Ví dụ: <script>alert("Hacked!")</script>
40+
- Để tránh XSS, sử dụng Rails helper như: sanitize, html_escape, raw
1441

42+
5. Distinguish: nil?, empty?, blank?, present?
43+
6. Presenting the effect of helper folder, what case of using it
1544

45+
- app/helpers/ chứa các module helper hỗ trợ xử lý logic hiển thị trong view.
46+
- Tác dụng: tách logic view và controller, dễ maintain, dễ debug,
47+
Giúp giữ view gọn gàng, tách biệt logic format ra khỏi template.

app/assets/stylesheets/application.css

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
*= require_tree .
3+
*= require_self
4+
*/

app/assets/stylesheets/custom.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "bootstrap";
2+
body {
3+
padding-top: 60px;
4+
}

0 commit comments

Comments
 (0)