質問編集履歴

1

書式の改善

2023/02/12 14:41

投稿

qoo12
qoo12

スコア0

test CHANGED
File without changes
test CHANGED
@@ -4,23 +4,116 @@
4
4
 
5
5
  ### 発生している問題・エラーメッセージ
6
6
  error画面
7
+ ```ここに言語を入力
8
+ Routing Error
7
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-12/3e79af1e-2add-47c8-8ada-8b83800669bd.png)
9
+ No route matches [GET] "/tweets/4/likes"
10
+
11
+ ```
8
12
 
9
13
 
10
14
  コマンドプロンプト rails routes実行画面
15
+ ```ここに言語を
16
+ tweet_likes POST /tweets/:tweet_id/likes(.:format) likes#create
17
+ tweet_like DELETE /tweets/:tweet_id/likes/:id(.:format) likes#destroy
18
+ tweets GET /tweets(.:format) tweets#index
19
+ POST /tweets(.:format) tweets#create
20
+ new_tweet GET /tweets/new(.:format) tweets#new
11
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-12/5fb9fea0-f78d-4575-98a0-cff3b3d55313.png)
21
+ edit_tweet GET /tweets/:id/edit(.:format) tweets#edit
22
+ tweet GET /tweets/:id(.:format) tweets#show
23
+ PATCH /tweets/:id(.:format) tweets#update
24
+ PUT /tweets/:id(.:format) tweets#update
25
+ DELETE /tweets/:id(.:format) tweets#destroy
26
+ ```
12
27
 
13
28
 
14
29
  routes.rb画面
30
+ ```ここに言語を入力
31
+ Rails.application.routes.draw do
32
+ devise_for :users
15
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-12/4bf71b07-6862-4811-beb8-e44cd3e96acd.png)
33
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
34
+
35
+ get 'hello/index' => 'hello#index'
36
+
37
+ get 'hello/link' => 'hello#link'
38
+
39
+ resources :tweets do
40
+ resources :likes, only: [:create, :destroy]
41
+
42
+ end
43
+
44
+ end
45
+ ```
16
46
 
17
47
 
18
48
  links_controller画面
49
+ ```ここに言語を入力
50
+ class LikesController < ApplicationController
51
+ def create
52
+ like = current_user.likes.create(tweet_id: params[:tweet_id])
53
+ redirect_back(fallback_location: root_path)
54
+ end
55
+
56
+ def destroy
19
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-12/2ed9a1ef-dd6e-4c65-a4ed-e9831a26e80e.png)
57
+ like = Like.find_by(tweet_id: params[:tweet_id], user_id: current_user.id)
58
+ like.destroy
59
+ redirect_back(fallback_location: root_path)
60
+ end
61
+
62
+ end
63
+ ```
20
64
 
21
65
 
22
66
  index.html画面
67
+ ```ここに言語を入力
68
+ <%= stylesheet_link_tag 'index', :media => "all" %>
69
+
70
+ <%= page_entries_info @tweets %>
71
+
72
+ <div class="tweets-container">
73
+ <h1>Geektwitter</h1>
74
+ <%= button_to 'ログアウト', destroy_user_session_path, method: :delete %>
75
+
76
+ <h3>Tweet一覧</h3>
77
+
78
+ <h3>投稿を検索</h3>
23
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-12/85d6a9d9-9474-4648-a738-13639c03a0f3.png)
79
+ <%= form_tag({controller:"tweets",action:"index"}, method: :get) do %>
80
+ <%= text_field_tag :search %>
81
+ <%= submit_tag '検索する' %>
82
+ <% end %>
83
+
84
+ <% @tweets.each do |t| %>
85
+ <div class="tweet">
86
+ <%= t.user.email %>
87
+
88
+ <div class="main-box">
89
+ <div class="left-container"><%= t.body %></div>
90
+ <%= image_tag t.image_url, size: "250x200" if t.image? %>
91
+ <% if user_signed_in? %>
92
+ <% if current_user.already_liked?(t) %>
93
+ <%= link_to tweet_like_path(id: t.id, tweet_id: t.id), method: :delete do %>
94
+ <i class="fas fa-heart"></i><%= t.likes.count %>
95
+ <% end %>
96
+ <% else %>
97
+ <%= link_to tweet_likes_path(id: t.id, tweet_id: t.id), method: :post do %>
98
+ <i class="far fa-heart"></i><%= t.likes.count %>
99
+ <% end %>
100
+ <% end %>
101
+ <% else %>
102
+ <i class="far fa-heart"></i><%= t.likes.count %>
103
+ <% end %>
104
+
105
+ <div class="right-container">
106
+ <%= link_to "詳細", tweet_path(t.id) %>
107
+ <%= link_to "編集", edit_tweet_path(t.id) %>
108
+ <%= link_to "削除", tweet_path(t.id), method: :delete %>
109
+ </div>
110
+ </div>
111
+ <p class="time"><%= t.created_at %></p>
112
+ </div>
113
+ <% end %>
114
+ <%= paginate @tweets %>
115
+ </div>
116
+ ```
24
117
 
25
118
 
26
119
  ### 試したこと