Skip to content

Commit b5e12ac

Browse files
committed
Lesson8 prototype
1 parent e6814fe commit b5e12ac

File tree

19 files changed

+155
-9
lines changed

19 files changed

+155
-9
lines changed

app/assets/javascripts/likes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Place all the behaviors and hooks related to the matching controller here.
2+
// All this logic will automatically be available in application.js.

app/assets/stylesheets/likes.scss

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.note-show .note .like {
2+
margin: 0;
3+
color: gray;
4+
}
5+
6+
.note-show .note .unlike {
7+
margin: 0;
8+
color: #bb428f;
9+
}
10+
11+
.likes-link {
12+
margin: 30px 0;
13+
}
14+
15+
.likes-link a {
16+
background-color: rgba(187, 66, 143, 0.92);
17+
color: white;
18+
padding: 10px;
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class LikesController < ApplicationController
2+
before_action :authenticate_user, only: [:create, :destroy]
3+
4+
def index
5+
@user = User.find_by(id: params[:user_id])
6+
end
7+
8+
def create
9+
@like = Like.new(user_id: @current_user.id, note_id: params[:note_id])
10+
@like.save
11+
flash[:notice] = 'お気に入りに登録しました'
12+
redirect_to "/notes/#{params[:note_id]}"
13+
end
14+
15+
def destroy
16+
@like = Like.find_by(user_id: @current_user.id, note_id: params[:note_id])
17+
@like.destroy
18+
flash[:notice] = 'お気に入りから削除しました'
19+
redirect_to "/notes/#{params[:note_id]}"
20+
end
21+
end

app/helpers/likes_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module LikesHelper
2+
end

app/models/like.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Like < ApplicationRecord
2+
belongs_to :user
3+
belongs_to :note
4+
5+
validates :user_id, presence: true
6+
validates :note_id, presence: true
7+
end

app/models/note.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Note < ApplicationRecord
22
belongs_to :user
3-
3+
has_many :likes, dependent: :destroy
4+
45
validates :title, presence: true
56
validates :content, presence: true
67
validates :image, presence: true

app/models/user.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class User < ApplicationRecord
22
has_many :notes
3-
3+
has_many :likes
4+
45
validates :name, presence: true
56
validates :email, presence: true, uniqueness: true
67
validates :password, presence: true

app/views/likes/create.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Likes#create</h1>
2+
<p>Find me in app/views/likes/create.html.erb</p>

app/views/likes/destroy.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Likes#destroy</h1>
2+
<p>Find me in app/views/likes/destroy.html.erb</p>

app/views/likes/index.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="notes-index">
2+
<h2>
3+
<a href="/users/<%= @user.id%>"><%= @user.name %></a>
4+
さんのお気に入り一覧
5+
</h2>
6+
<% @user.likes.each do |like| %>
7+
<% note = like.note %>
8+
<div class="notes-list-item">
9+
<img src="<%= note.image %>">
10+
<h3>
11+
<a href="/notes/<%= note.id %>">
12+
<%= note.title %>
13+
</a>
14+
</h3>
15+
</div>
16+
<% end %>
17+
</div>

0 commit comments

Comments
 (0)