teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

9

変更

2020/01/16 09:53

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -146,6 +146,7 @@
146
146
 
147
147
  def index
148
148
  @post_images = PostImage.page(params[:page]).reverse_order
149
+ @post_image = PostImage.new
149
150
  @user = @post_image.user
150
151
  end
151
152
 

8

変更

2020/01/16 09:53

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -146,7 +146,7 @@
146
146
 
147
147
  def index
148
148
  @post_images = PostImage.page(params[:page]).reverse_order
149
- @user = PostImage.user
149
+ @user = @post_image.user
150
150
  end
151
151
 
152
152
  def show

7

問題時のコードに変更

2020/01/16 09:34

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -100,33 +100,33 @@
100
100
  before_action :set_user
101
101
 
102
102
  def create
103
- @user = User.find(params[:relationship][:follow_id])
103
+ user = User.find(params[:relationship][:follow_id])
104
104
  following = current_user.follow(user)
105
105
  if following.save
106
106
  flash[:success] = 'ユーザーをフォローしました'
107
- redirect_to @user
107
+ redirect_to user
108
108
  else
109
109
  flash.now[:alert] = 'ユーザーのフォローに失敗しました'
110
- redirect_to @user
110
+ redirect_to user
111
111
  end
112
112
  end
113
113
 
114
114
  def destroy
115
- @user = User.find(params[:relationship][:follow_id])
115
+ user = User.find(params[:relationship][:follow_id])
116
116
  following = current_user.unfollow(user)
117
117
  if following.destroy
118
118
  flash[:success] = 'ユーザーのフォローを解除しました'
119
- redirect_to @user
119
+ redirect_to user
120
120
  else
121
121
  flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
122
- redirect_to @user
122
+ redirect_to user
123
123
  end
124
124
  end
125
125
 
126
126
  private
127
127
 
128
128
  def set_user
129
- @user = User.find(params[:relationship][:follow_id])
129
+ user = User.find(params[:relationship][:follow_id])
130
130
  end
131
131
  end
132
132
  ```
@@ -146,7 +146,7 @@
146
146
 
147
147
  def index
148
148
  @post_images = PostImage.page(params[:page]).reverse_order
149
- @user = @post_image.id
149
+ @user = PostImage.user
150
150
  end
151
151
 
152
152
  def show
@@ -181,7 +181,7 @@
181
181
  end
182
182
 
183
183
  def update
184
- @user = User.finr(params[:id])
184
+ @user = User.find(params[:id])
185
185
  @user.update(user_params)
186
186
  redirect_to user_path(@user.id)
187
187
  end
@@ -192,6 +192,7 @@
192
192
  params.requrie(:user).permit(:name, :profile_image)
193
193
  end
194
194
  end
195
+
195
196
  ```
196
197
  **【部分テンプレート】**
197
198
  **relationships/_follow_button.html.erb**
@@ -199,12 +200,12 @@
199
200
  <% unless current_user == user %>
200
201
  <% if current_user.following?(user) %>
201
202
  <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
202
- <%= f.hidden_field :follow_id, user.id %>
203
+ <%= hidden_field_tag :follow_id, user.id %>
203
204
  <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
204
205
  <% end %>
205
206
  <% else %>
206
207
  <%= form_for(current_user.relationships.build) do |f| %>
207
- <%= f.hidden_field :follow_id, user.id %>
208
+ <%= hidden_field_tag :follow_id, user.id %>
208
209
  <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
209
210
  <% end %>
210
211
  <% end %>

6

relationships_controller,_follow_button.html.erbの記述変更

2020/01/16 09:32

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -100,33 +100,33 @@
100
100
  before_action :set_user
101
101
 
102
102
  def create
103
- user = User.find(params[:relationship][:follow_id])
103
+ @user = User.find(params[:relationship][:follow_id])
104
104
  following = current_user.follow(user)
105
105
  if following.save
106
106
  flash[:success] = 'ユーザーをフォローしました'
107
- redirect_to user
107
+ redirect_to @user
108
108
  else
109
109
  flash.now[:alert] = 'ユーザーのフォローに失敗しました'
110
- redirect_to user
110
+ redirect_to @user
111
111
  end
112
112
  end
113
113
 
114
114
  def destroy
115
- user = User.find(params[:relationship][:follow_id])
115
+ @user = User.find(params[:relationship][:follow_id])
116
116
  following = current_user.unfollow(user)
117
117
  if following.destroy
118
118
  flash[:success] = 'ユーザーのフォローを解除しました'
119
- redirect_to user
119
+ redirect_to @user
120
120
  else
121
121
  flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
122
- redirect_to user
122
+ redirect_to @user
123
123
  end
124
124
  end
125
125
 
126
126
  private
127
127
 
128
128
  def set_user
129
- user = User.find(params[:relationship][:follow_id])
129
+ @user = User.find(params[:relationship][:follow_id])
130
130
  end
131
131
  end
132
132
  ```
@@ -199,12 +199,12 @@
199
199
  <% unless current_user == user %>
200
200
  <% if current_user.following?(user) %>
201
201
  <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
202
- <%= hidden_field_tag :follow_id, user.id %>
202
+ <%= f.hidden_field :follow_id, user.id %>
203
203
  <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
204
204
  <% end %>
205
205
  <% else %>
206
206
  <%= form_for(current_user.relationships.build) do |f| %>
207
- <%= hidden_field_tag :follow_id, user.id %>
207
+ <%= f.hidden_field :follow_id, user.id %>
208
208
  <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
209
209
  <% end %>
210
210
  <% end %>

5

indexアクションでの表記変更

2020/01/16 09:14

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -146,7 +146,7 @@
146
146
 
147
147
  def index
148
148
  @post_images = PostImage.page(params[:page]).reverse_order
149
- @user = PostImage.find(params[:id])
149
+ @user = @post_image.id
150
150
  end
151
151
 
152
152
  def show

4

PostImage.rbの追加

2020/01/16 08:39

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -76,6 +76,23 @@
76
76
  end
77
77
  end
78
78
  ```
79
+ post_image.rb
80
+ ```
81
+ class PostImage < ApplicationRecord
82
+ belongs_to :user
83
+
84
+ has_many :favorites, dependent: :destroy
85
+ has_many :fav_users, through: :favorites, source: :user
86
+ has_many :cosplay_favorites, dependent: :destroy
87
+ has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user
88
+ has_many :post_comments, dependent: :destroy
89
+
90
+ attachment :real_image
91
+ attachment :cosplay_image
92
+
93
+ default_scope -> { order(created_at: :asc) }
94
+ end
95
+ ```
79
96
  【Controller】
80
97
  **relationship_controller.rb**
81
98
  ```

3

users_controller.rbの追加

2020/01/16 03:00

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -151,6 +151,31 @@
151
151
  end
152
152
 
153
153
  ```
154
+ **users_controller.rb**
155
+ ```
156
+ class UsersController < ApplicationController
157
+ def show
158
+ @user = User.find(params[:id])
159
+ @post_images = @user.post_images.page(params[:page]).reverse_order
160
+ end
161
+
162
+ def edit
163
+ @user = User.find(params[:id])
164
+ end
165
+
166
+ def update
167
+ @user = User.finr(params[:id])
168
+ @user.update(user_params)
169
+ redirect_to user_path(@user.id)
170
+ end
171
+
172
+ private
173
+
174
+ def user_params
175
+ params.requrie(:user).permit(:name, :profile_image)
176
+ end
177
+ end
178
+ ```
154
179
  **【部分テンプレート】**
155
180
  **relationships/_follow_button.html.erb**
156
181
  ```

2

コントローラーの追記

2020/01/16 00:54

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -116,42 +116,40 @@
116
116
  **postimages_controller.rb**
117
117
  ```
118
118
  class PostImagesController < ApplicationController
119
-
120
- def new
119
+ def new
121
- @post_image = PostImage.new
120
+ @post_image = PostImage.new
122
121
  end
123
-
122
+
124
123
  def create
125
124
  @post_image = PostImage.new(post_image_params)
126
125
  @post_image.user_id = current_user.id
127
- if @post_image.save
126
+ @post_image.save
128
- redirect_to post_images_path
127
+ redirect_to post_images_path
129
- else
130
- render :new
131
- end
132
128
  end
133
-
129
+
134
130
  def index
135
- @post_images = PostImage.page(params[:page]).reverse_order
131
+ @post_images = PostImage.page(params[:page]).reverse_order
132
+ @user = PostImage.find(params[:id])
136
- end
133
+ end
137
-
134
+
138
135
  def show
139
- @post_image = PostImage.find(params[:id])
136
+ @post_image = PostImage.find(params[:id])
140
- @post_comment = PostComment.new
137
+ @post_comment = PostComment.new
141
- end
138
+ end
142
-
139
+
143
- def destroy
140
+ def destroy
144
- @post_image = PostImage.find(params[:id])
141
+ @post_image = PostImage.find(params[:id])
145
- @post_image.destroy
142
+ @post_image.destroy
146
- redirect_to post_images_path
143
+ redirect_to post_images_path
147
- end
144
+ end
148
-
145
+
149
146
  private
150
-
147
+
151
148
  def post_image_params
152
- params.require(:post_image).permit(:shop_name, :image, :caption)
149
+ params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count)
153
150
  end
154
151
  end
152
+
155
153
  ```
156
154
  **【部分テンプレート】**
157
155
  **relationships/_follow_button.html.erb**

1

コントローラーの追加

2020/01/16 00:52

投稿

is02
is02

スコア17

title CHANGED
File without changes
body CHANGED
@@ -113,6 +113,46 @@
113
113
  end
114
114
  end
115
115
  ```
116
+ **postimages_controller.rb**
117
+ ```
118
+ class PostImagesController < ApplicationController
119
+
120
+ def new
121
+ @post_image = PostImage.new
122
+ end
123
+
124
+ def create
125
+ @post_image = PostImage.new(post_image_params)
126
+ @post_image.user_id = current_user.id
127
+ if @post_image.save
128
+ redirect_to post_images_path
129
+ else
130
+ render :new
131
+ end
132
+ end
133
+
134
+ def index
135
+ @post_images = PostImage.page(params[:page]).reverse_order
136
+ end
137
+
138
+ def show
139
+ @post_image = PostImage.find(params[:id])
140
+ @post_comment = PostComment.new
141
+ end
142
+
143
+ def destroy
144
+ @post_image = PostImage.find(params[:id])
145
+ @post_image.destroy
146
+ redirect_to post_images_path
147
+ end
148
+
149
+ private
150
+
151
+ def post_image_params
152
+ params.require(:post_image).permit(:shop_name, :image, :caption)
153
+ end
154
+ end
155
+ ```
116
156
  **【部分テンプレート】**
117
157
  **relationships/_follow_button.html.erb**
118
158
  ```