質問編集履歴
2
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,353 +3,3 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
undefined method `username' for nil:NilClassが治りません。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
![イメージ説明](b72fe4bf981d7ea863c5698c2616f32a.png)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
やりたいこととしては、記事のリンクを「/ユーザー名/posts/記事のID」にしたいです。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
また、記事に直接URLでアクセスしたら、
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
get ':username/posts/:id' => 'posts#show', as: :test_show
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
「http://localhost:3000/ユーザー名/posts/記事ID」
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
でアクセスできるようにしたいのですが、「http://localhost:3000/ユーザー名/posts/記事ID」のユーザー名部分に適当な文字などを入力しても、同じ記事にアクセスできてしまう問題を解決したいです。
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
# posts/index.html.erb
|
42
|
-
|
43
|
-
```ruby
|
44
|
-
|
45
|
-
<h1>全ての投稿一覧</h1>
|
46
|
-
|
47
|
-
<ul>
|
48
|
-
|
49
|
-
<% @posts.each do |post| %>
|
50
|
-
|
51
|
-
<li>
|
52
|
-
|
53
|
-
<%= post.title %>(<%= post.set_created_date %>) | <%= link_to '詳細', test_show_path(username: @user.username, id: post.public_uid) %>
|
54
|
-
|
55
|
-
</li>
|
56
|
-
|
57
|
-
<% end %>
|
58
|
-
|
59
|
-
</ul>
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
```
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
# posts/show.html.erb
|
68
|
-
|
69
|
-
```ruby
|
70
|
-
|
71
|
-
<h1><%= @post.title %></h1>
|
72
|
-
|
73
|
-
<div>ユーザー名:@<%= link_to @user.username, "/#{@user.username}" %></div>
|
74
|
-
|
75
|
-
<div>投稿日:<%= @post.set_created_date %></div>
|
76
|
-
|
77
|
-
<div>更新日:<%= @post.set_updated_date %></div>
|
78
|
-
|
79
|
-
<div>記事URL:<%= @post.public_uid %></div>
|
80
|
-
|
81
|
-
<div><%= markdown(@post.content).html_safe %></div>
|
82
|
-
|
83
|
-
<br>
|
84
|
-
|
85
|
-
<%= link_to 'Twitterでシェア', "https://twitter.com/share?url=#{ request.url }&text=#{ @post.title }&hashtags=テスト", title: 'Twitter', target: '_blank' %>
|
86
|
-
|
87
|
-
<%= link_to 'LINEでシェア', "https://social-plugins.line.me/lineit/share?url=#{ request.url }", title: 'LINE', target: '_blank' %>
|
88
|
-
|
89
|
-
<%= link_to 'Facebookでシェア', "http://www.facebook.com/share.php?u=#{ request.url }", title: 'Facebook', target: '_blank' %>
|
90
|
-
|
91
|
-
<br>
|
92
|
-
|
93
|
-
<%= link_to '一覧に戻る', posts_path %>
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
```
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
# routes.rb
|
102
|
-
|
103
|
-
```ruby
|
104
|
-
|
105
|
-
Rails.application.routes.draw do
|
106
|
-
|
107
|
-
devise_for :users, controllers: {
|
108
|
-
|
109
|
-
:registrations => 'users/registrations',
|
110
|
-
|
111
|
-
:sessions => 'users/sessions',
|
112
|
-
|
113
|
-
:passwords => 'users/passwords'
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
root 'pages#index'
|
120
|
-
|
121
|
-
resources :posts, except:[:show]
|
122
|
-
|
123
|
-
get ':username/posts/:id' => 'posts#show', as: :test_show
|
124
|
-
|
125
|
-
get ':username' => 'pages#show', as: :pages_show
|
126
|
-
|
127
|
-
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
```
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
#posts_controller.rb
|
138
|
-
|
139
|
-
```ruby
|
140
|
-
|
141
|
-
class PostsController < ApplicationController
|
142
|
-
|
143
|
-
# before_action :authenticate_user!
|
144
|
-
|
145
|
-
before_action :sign_in_required, only: [:new, :create, :edit, :update, :destroy]
|
146
|
-
|
147
|
-
before_action :baria_user, only: [:edit, :destroy, :update]
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
def index
|
152
|
-
|
153
|
-
@posts = Post.all.order(id: "DESC")
|
154
|
-
|
155
|
-
@user = User.find_by(username: params[:username])
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def show
|
162
|
-
|
163
|
-
@post = Post.find_by(public_uid: params[:id])
|
164
|
-
|
165
|
-
@user = User.find_by(id: @post.user_id)
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
def new
|
172
|
-
|
173
|
-
@post = Post.new
|
174
|
-
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
def create
|
180
|
-
|
181
|
-
@post = Post.new(post_params)
|
182
|
-
|
183
|
-
if @post.save
|
184
|
-
|
185
|
-
redirect_to post_path(@post.public_uid)
|
186
|
-
|
187
|
-
else
|
188
|
-
|
189
|
-
render 'new'
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
def edit
|
198
|
-
|
199
|
-
@post = Post.find_by(public_uid: params[:id])
|
200
|
-
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
def update
|
206
|
-
|
207
|
-
@post = Post.find(params[:id])
|
208
|
-
|
209
|
-
if @post.update(post_params)
|
210
|
-
|
211
|
-
redirect_to post_path(@post.public_uid)
|
212
|
-
|
213
|
-
else
|
214
|
-
|
215
|
-
render 'edit'
|
216
|
-
|
217
|
-
end
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
def destroy
|
224
|
-
|
225
|
-
@post = Post.find_by(public_uid: params[:id])
|
226
|
-
|
227
|
-
@post.destroy
|
228
|
-
|
229
|
-
redirect_to posts_path
|
230
|
-
|
231
|
-
end
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
private
|
236
|
-
|
237
|
-
# 仮に悪意のあるリクエスト(指定した以外のデータを送ってくる等)を受けた際に、.permitメソッドで許可していない項目については変更されず、データの扱いがより安全になります。
|
238
|
-
|
239
|
-
def post_params
|
240
|
-
|
241
|
-
params.require(:post).permit(:title, :content).merge(user_id: current_user.id)
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
def baria_user
|
248
|
-
|
249
|
-
unless Post.find_by(public_uid: params[:id]).user_id == current_user.id
|
250
|
-
|
251
|
-
flash[:notice] = "権限がありません"
|
252
|
-
|
253
|
-
redirect_to posts_path
|
254
|
-
|
255
|
-
end
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
```
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
#user.rb
|
268
|
-
|
269
|
-
```ruby
|
270
|
-
|
271
|
-
class User < ApplicationRecord
|
272
|
-
|
273
|
-
has_many :posts, dependent: :destroy
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
# Include default devise modules. Others available are:
|
278
|
-
|
279
|
-
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
280
|
-
|
281
|
-
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable, :timeoutable, :trackable
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
validates :username,
|
286
|
-
|
287
|
-
uniqueness: true,
|
288
|
-
|
289
|
-
length: { minimum: 3, maximum: 25 }
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
def to_param
|
294
|
-
|
295
|
-
return self.username
|
296
|
-
|
297
|
-
end
|
298
|
-
|
299
|
-
end
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
```
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
#post.rb
|
308
|
-
|
309
|
-
```ruby
|
310
|
-
|
311
|
-
class Post < ApplicationRecord
|
312
|
-
|
313
|
-
belongs_to :users, optional: true
|
314
|
-
|
315
|
-
generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(20)
|
316
|
-
|
317
|
-
validates :title,
|
318
|
-
|
319
|
-
presence: { message: 'は空白にできません'},
|
320
|
-
|
321
|
-
length: { minimum: 5, maximum: 40 }
|
322
|
-
|
323
|
-
validates :content,
|
324
|
-
|
325
|
-
presence: true,
|
326
|
-
|
327
|
-
length: { minimum: 120 }
|
328
|
-
|
329
|
-
validates :user_id,
|
330
|
-
|
331
|
-
presence: true
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
def set_created_date
|
336
|
-
|
337
|
-
created_at.strftime("%Y/%m/%d %H:%M")
|
338
|
-
|
339
|
-
end
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
def set_updated_date
|
344
|
-
|
345
|
-
updated_at.strftime("%Y/%m/%d %H:%M")
|
346
|
-
|
347
|
-
end
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
end
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
```
|
1
更新
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
NoMethodError in Posts#indexが解決できない
|
1
|
+
nNoMethodError in Posts#indexが解決できない
|
test
CHANGED
@@ -6,6 +6,10 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
![イメージ説明](b72fe4bf981d7ea863c5698c2616f32a.png)
|
10
|
+
|
11
|
+
|
12
|
+
|
9
13
|
やりたいこととしては、記事のリンクを「/ユーザー名/posts/記事のID」にしたいです。
|
10
14
|
|
11
15
|
|