-
Notifications
You must be signed in to change notification settings - Fork 0
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
Upload image with paperclip and usage postgres database #5
Conversation
app/controllers/albums_controller.rb
Outdated
def create | ||
@album = current_user.albums.new album_params | ||
if @album.save | ||
if params[:images] |
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ếu tạo album thành công mà không có hình ảnh nào hết thì sao? Khi đó code của em chưa thấy xử lý trường hợp đó
app/controllers/albums_controller.rb
Outdated
def my_albums | ||
@albums = current_user.albums.page params[:page] | ||
render :index | ||
# render :index |
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.
Dư code bị comment
app/controllers/feeds_controller.rb
Outdated
@@ -2,6 +2,6 @@ class FeedsController < ApplicationController | |||
layout 'layout_album_photo' | |||
|
|||
def index | |||
redirect_to photos_path | |||
# redirect_to photos_path |
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.
Dư code bị comment
app/controllers/photos_controller.rb
Outdated
render :new | ||
end | ||
end | ||
|
||
def my_photos | ||
@photos = current_user.photos.page params[:page] | ||
# render :index |
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.
Dư commented code
@@ -1,30 +1,31 @@ | |||
class SessionsController < Devise::SessionsController | |||
class SessionsController < Devise::SessionsController |
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.
Có cần phải override file SessionsController hay không? Tại sao cần phải override?
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.
em override vì sau này khi đăng nhập xong e thêm flash với lại render hay redirect_to , mà em chỉ mới thêm vào chưa xử lý.
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.
Tại sao em cần thêm Flash với lại render? Có yêu cầu gì đặc biệt hay không?
app/views/photos/new.html.erb
Outdated
<%= f.text_area :description, class: 'form-control' %> | ||
<%= f.label :image %> | ||
<%= f.file_field :image %> | ||
<%= f.submit %> |
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.
Button cần có class của Bootstrap
app/views/photos/my_photos.html.erb
Outdated
@@ -1,3 +1,5 @@ | |||
<%= button_to "Add Photo", { action: "new" }, method: :get, class: 'btn-primary upload' %> |
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.
Thiếu class btn
app/views/photos/new.html.erb
Outdated
<% end %> | ||
</ul> | ||
<% end %> | ||
<%= f.label :title %> |
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.
Form như thế này là chưa đúng kiểu của bootstrap, cần xem trên trang Bootstrap form tạo ra có những thẻ gì, class như thế nào và làm tương ứng. Ví dụ <div class="form-group">
app/views/albums/new.html.erb
Outdated
<% end %> | ||
</ul> | ||
<% end %> | ||
<%= f.label :title %> |
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.
Form như thế này là chưa đúng kiểu của bootstrap, cần xem trên trang Bootstrap form tạo ra có những thẻ gì, class như thế nào và làm tương ứng. Ví dụ <div class="form-group">
@@ -0,0 +1,9 @@ | |||
class ChangeColumnPhoto < ActiveRecord::Migration[5.1] | |||
def up | |||
change_column :photos, :album_id, :integer |
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.
Tại sao up và down đều giống như nhau? Nếu vậy thì migration này dùng để làm gì?
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.
cái này bữa e test dùng để set cho khóa ngoại bằng 0 để add data, e xóa cái thuộc tính trong def down rồi. file này không cần thiết a.
app/views/albums/my_albums.html.erb
Outdated
@@ -0,0 +1,8 @@ | |||
<%= link_to new_album_path do %> | |||
<button type="button" class="btn btn-primary">Add Album</button> |
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.
Chưa dùng i18n
app/views/albums/new.html.erb
Outdated
<hr> | ||
<%= form_for @album, html: { multipart: true } do |f| %> | ||
<% if @album.errors.any? %> | ||
<h3>Some errors were found:</h3> |
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.
Chưa dùng i18n
app/views/photos/my_photos.html.erb
Outdated
@@ -1,3 +1,7 @@ | |||
<%= link_to new_photo_path do %> | |||
<button type="button" class="btn btn-primary">Add Photo</button> |
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.
Chưa dùng i18n
app/views/photos/new.html.erb
Outdated
@@ -0,0 +1,29 @@ | |||
<h3 class="text-primary">New Photo</h3> |
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.
Chưa dùng i18n
app/views/photos/new.html.erb
Outdated
<hr> | ||
<%= form_for @photo do |f| %> | ||
<% if @photo.errors.any? %> | ||
<h3>Some errors were found:</h3> |
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.
Chưa dùng i18n
app/assets/stylesheets/custom.scss
Outdated
.navbar { | ||
background-color: blue; | ||
.nav > li.dropdown > a { | ||
color: #fff; |
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.
Indentation lộn xộn quá
app/assets/stylesheets/custom.scss
Outdated
@@ -8,25 +8,42 @@ | |||
font-size: 1.7em; | |||
color: #fff; | |||
text-transform: uppercase; | |||
padding-top: 9px; | |||
align-content: center; |
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.
Đây là thuộc tính dùng cho flex, e có đang dùng đúng không vậy? https://www.w3schools.com/cssref/css3_pr_align-content.asp
app/assets/stylesheets/custom.scss
Outdated
text-align: center; | ||
width: 100%; | ||
display: inline; | ||
} | ||
div.caption p { |
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.
Chưa tận dụng được sức mạnh của SCSS. Em có thể khai báo như sau
div.caption {
p {
}
span {
}
}
app/assets/stylesheets/custom.scss
Outdated
top: 75%; | ||
left: 84%; | ||
} | ||
i { |
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.
Em override nguyên thẻ luôn sao? Tại sao lại làm như vậy? Thẻ i thì nó là để in nghiêng, không được thay đổi các thuộc tính của nó như vậy
app/controllers/albums_controller.rb
Outdated
@@ -1,5 +1,6 @@ | |||
class AlbumsController < ApplicationController | |||
layout 'layout_album_photo', only: :index | |||
before_action :load_image, only: :create |
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.
Hàm của em đặt tên không đúng, theo nội dung a đọc thì hàm đó e dùng để check có params[:images] được gửi lên server hay không, cần rename hàm cho đúng
app/assets/stylesheets/custom.scss
Outdated
top: 5px; | ||
font-size: 15px; | ||
} | ||
span:hover { |
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.
Cái này có thể để ngay trong thẻ span
ở trên, như sau
span {
&:hover{
}
}
app/controllers/albums_controller.rb
Outdated
|
||
def check_image | ||
@images = params[:album][:images] | ||
return if @images |
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.
Logic này hơi khó hiểu cho người đọc, nếu là check coi có params image hay không thì nên viết logic như sau
unless @images
redirect_to new_album_path
end
app/controllers/photos_controller.rb
Outdated
def destroy | ||
@photo.destroy | ||
redirect_to my_photos_path | ||
end | ||
def my_photos |
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.
Cần phân tách 2 methods bằng một dòng trống
app/models/photo.rb
Outdated
has_attached_file :image, styles: { thumb: ["200x150#", :png] } | ||
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\z/ }, presence: true | ||
scope :order_by_created_at, -> { order(created_at: :desc) } | ||
scope :photo_pucblic, -> { where(share_mode: true) } |
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 chính tả pucblic
. Và scope này đã nằm trong model Photo nên không cần phải có chữ photo_
ở đầu nữa, chỉ cần đặt scope :public
là được
app/views/albums/_album.html.erb
Outdated
@@ -2,20 +2,17 @@ | |||
<% albums.each do |album| %> | |||
<div class="col-md-6"> | |||
<div class="post-image"> | |||
<div class="pop"><%= image_tag("image2.jpg",size: "200x200") %></div> | |||
<div class="pop"><%= image_tag("image2.jpg",size: "200x200", class: '') %></div> |
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.
Class rỗng để làm gì? Nếu không có class thì không cần chỉ định gì cả
app/views/layouts/_header.html.erb
Outdated
<% else %> | ||
<li><%= link_to "Log in", new_user_session_path %></li> | ||
<li><%= link_to "Log in", new_user_session_path %></li> |
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.
Chưa dùng i18n
app/views/photos/_photo.html.erb
Outdated
<div class="post-data"> | ||
<%= image_tag("image2.jpg", class: 'avatar') %> | ||
<a href="#" class="post-catagory"><%= User.find(photo.user_id).fullname %></a> | ||
<h5 class="post-name text-primary" ><%= User.find(photo.user_id).fullname %></h5> |
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.
Không được dùng User.find như thế này trong view, cần dùng association
app/views/photos/_photo.html.erb
Outdated
</div> | ||
<span class="post-like glyphicon glyphicon-heart"></span> | ||
<span class="number-like">123</span> | ||
<span class="post-date"><%= Time.zone.at(photo.created_at).strftime("%I:%M %p %m/%d/%Y") %></span> |
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.
Đưa hàm format này vào 1 helper, chỗ khác cũng dùng lại được. Khi cần format thì chỉ cần gọi như sau
<%= format_time(photo.created_at) %>
app/views/photos/_photo.html.erb
Outdated
</div> | ||
<% if index == 1 %> |
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.
Logic khúc này nữa là sao? Tại sao nếu index là 1 thì sẽ có các thẻ div như vậy?
app/views/photos/my_photos.html.erb
Outdated
<% end %> | ||
</div> | ||
</div> | ||
<% if index == 3 %> |
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.
Logic này để làm gì?
app/controllers/albums_controller.rb
Outdated
|
||
def index | ||
@albums = Album.page params[:page] | ||
@albums = Album.album_public.order_by_created_at.page params[:page] |
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.
scope trong album thì không cần prefix 'album_' nữa
app/controllers/albums_controller.rb
Outdated
|
||
def destroy | ||
@album.destroy | ||
flash[:success] = "deleted success !" |
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.
Chưa có i18n cho message
app/models/photo.rb
Outdated
validates :title, length: { maximum: 140 }, presence: true, if: :check_album_id? | ||
validates :description, length: { maximum: 300 }, presence: true, if: :check_album_id? | ||
scope :order_by_created_at, -> { order(created_at: :desc) } | ||
scope :photo_public, -> { where(share_mode: true) } |
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.
Không cần để tiền tố là photo_
trong scope này, chỉ cần để public
là được
app/models/album.rb
Outdated
validates :title, length: { maximum: 140 }, presence: true | ||
validates :description, length: { maximum: 300 }, presence: true | ||
scope :order_by_created_at, -> { order(created_at: :desc) } | ||
scope :album_public, -> { where(share_mode: true) } |
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.
Không cần để album_
trong scope này
app/models/photo.rb
Outdated
scope :order_by_created_at, -> { order(created_at: :desc) } | ||
scope :photo_public, -> { where(share_mode: true) } | ||
|
||
def check_album_id? |
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 đặt tên hàm là no_album?
hoặc album_is_empty?
<div class="thumbnail"> | ||
<% if album.photos.present? %> | ||
<% album.photos.order_by_created_at.each_with_index do |image, index| %> | ||
<% break if index == 3 %> |
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 đích đoạn if này là gì?
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.
số 3 này là em mún cho cái album chỉ show tối đa 3 ảnh trong cái album có số ảnh phía trong ấy a
để e đặt lại biến constant
</div> | ||
<div class="form-group"> | ||
<%= f.label :last_name %> | ||
<%= f.text_field :last_name, autofocus: true, placeholder: t('.last_name'), class: 'form-control' %> |
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.
Dư autofocus
</div> | ||
<div class="form-group"> | ||
<%= f.label :email %> | ||
<%= f.email_field :email, autofocus: true, placeholder: t('.email'), class: 'form-control' %> |
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.
Dư autofocus
</ul> | ||
</li> | ||
<li> | ||
<%= link_to t('logout'), destroy_user_session_path, method: :delete %> |
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 dùng lazy lookup thay vì đặt key logout
ở level 1 như trong file locales.
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.
có mấy cái chung em hay dùng em để ở mục ngoài cùng không biết đc không anh
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.
Cũng được nếu như em đã hiểu khi nào cần lazy lookup, khi nào không
<% else %> | ||
<li><%= link_to "Log in", new_user_session_path %></li> | ||
<li><%= link_to t('login'), new_user_session_path %></li> |
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 dùng lazy lookup thay vì đặt key login
ở level 1 như trong file locales.
@@ -1,3 +1,4 @@ | |||
<%= csrf_meta_tags %> | |||
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta> |
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.
A có xem lại thì thẻ sẽ không cần thẻ đóng, em remove thẻ đóng đi
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %> | ||
</div> | ||
<div class="form-group"> | ||
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> |
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.
Chưa dùng i18n
<div class="form-group"> | ||
<%= f.label :password_confirmation %> | ||
<% if @minimum_password_length %> | ||
<em>(<%= @minimum_password_length %> characters minimum) </em> |
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.
Chưa dùng i18n
<% end %> | ||
</div> | ||
<div class="form-group"> | ||
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> |
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.
Chưa dùng i18n
</div> | ||
<div class="form-group"> | ||
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> | ||
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> |
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.
Chưa dùng i18n
|
||
def update_images | ||
self.photos.delete_all | ||
@images.each do |image| |
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 gọi hàm create_images
bên trong này thay vì viết lại logic tạo images
No description provided.