質問編集履歴
1
変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rails URLを/ユーザー名/posts/記事の:idにしたい
|
1
|
+
んこうrails URLを/ユーザー名/posts/記事の:idにしたい
|
test
CHANGED
@@ -2,282 +2,8 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
ユーザーごとの投稿の結びつけなどはできており、今回の目的は、現状の投稿した記事のURL
|
5
|
+
ユーザーごとの投稿の結びつけなどはできており、今回の目的は、現状の投稿した記事のURLにしたいと考えています。
|
6
6
|
|
7
7
|
|
8
8
|
|
9
9
|
調べたり、いろいろ試したのですが、実装できなかったので、詳しくわかる方に教えていただきたいです。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
routes.rb
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
|
17
|
-
Rails.application.routes.draw do
|
18
|
-
|
19
|
-
# パスワード確認トークン用URL:サイトURL/users/confirmation?confirmation_token=トークン
|
20
|
-
|
21
|
-
devise_for :users, controllers: {
|
22
|
-
|
23
|
-
:registrations => 'users/registrations',
|
24
|
-
|
25
|
-
:sessions => 'users/sessions',
|
26
|
-
|
27
|
-
:passwords => 'users/passwords'
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
root 'pages#index'
|
32
|
-
|
33
|
-
# get 'posts' => 'posts#index', as: :posts
|
34
|
-
|
35
|
-
# get 'posts/:id' => 'posts#show', as: :post
|
36
|
-
|
37
|
-
# get 'posts/new' => 'posts#new', as: :new_post
|
38
|
-
|
39
|
-
# post 'posts' => 'posts#create'
|
40
|
-
|
41
|
-
# get 'posts/edit/:id' => 'posts#edit', as: :edit_post
|
42
|
-
|
43
|
-
# patch 'posts/:id' => 'posts#update'
|
44
|
-
|
45
|
-
# delete 'posts/:id' => 'posts#destroy'
|
46
|
-
|
47
|
-
resources :posts #, only:[:index, :create, :new, :edit, :update, :destroy]
|
48
|
-
|
49
|
-
get ':username' => 'pages#show', as: :pages_show
|
50
|
-
|
51
|
-
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
```
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
pages_controller.rb
|
62
|
-
|
63
|
-
```ruby
|
64
|
-
|
65
|
-
class PagesController < ApplicationController
|
66
|
-
|
67
|
-
def index
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
def show
|
74
|
-
|
75
|
-
@user = User.find_by(username: params[:username])
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
```
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
posts_controller.rb
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
|
89
|
-
class PostsController < ApplicationController
|
90
|
-
|
91
|
-
# before_action :authenticate_user!
|
92
|
-
|
93
|
-
before_action :sign_in_required, only: [:new, :create, :edit, :update, :destroy]
|
94
|
-
|
95
|
-
before_action :baria_user, only: [:edit, :destroy, :update]
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def index
|
100
|
-
|
101
|
-
@posts = Post.all.order(id: "DESC")
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
def show
|
108
|
-
|
109
|
-
@post = Post.find_by(public_uid: params[:id])
|
110
|
-
|
111
|
-
@user = User.find_by(id: @post.user_id)
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def new
|
118
|
-
|
119
|
-
@post = Post.new
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
def create
|
126
|
-
|
127
|
-
@post = Post.new(post_params)
|
128
|
-
|
129
|
-
if @post.save
|
130
|
-
|
131
|
-
redirect_to post_path(@post.public_uid)
|
132
|
-
|
133
|
-
else
|
134
|
-
|
135
|
-
render 'new'
|
136
|
-
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
def edit
|
144
|
-
|
145
|
-
@post = Post.find_by(public_uid: params[:id])
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
def update
|
152
|
-
|
153
|
-
@post = Post.find(params[:id])
|
154
|
-
|
155
|
-
if @post.update(post_params)
|
156
|
-
|
157
|
-
redirect_to post_path(@post.public_uid)
|
158
|
-
|
159
|
-
else
|
160
|
-
|
161
|
-
render 'edit'
|
162
|
-
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
def destroy
|
170
|
-
|
171
|
-
@post = Post.find_by(public_uid: params[:id])
|
172
|
-
|
173
|
-
@post.destroy
|
174
|
-
|
175
|
-
redirect_to posts_path
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
private
|
182
|
-
|
183
|
-
# 仮に悪意のあるリクエスト(指定した以外のデータを送ってくる等)を受けた際に、.permitメソッドで許可していない項目については変更されず、データの扱いがより安全になります。
|
184
|
-
|
185
|
-
def post_params
|
186
|
-
|
187
|
-
params.require(:post).permit(:title, :content).merge(user_id: current_user.id)
|
188
|
-
|
189
|
-
end
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
def baria_user
|
194
|
-
|
195
|
-
unless Post.find_by(public_uid: params[:id]).user_id == current_user.id
|
196
|
-
|
197
|
-
flash[:notice] = "権限がありません"
|
198
|
-
|
199
|
-
redirect_to posts_path
|
200
|
-
|
201
|
-
end
|
202
|
-
|
203
|
-
end
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
```
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
user.rb
|
214
|
-
|
215
|
-
```ruby
|
216
|
-
|
217
|
-
class User < ApplicationRecord
|
218
|
-
|
219
|
-
has_many :posts, dependent: :destroy
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
# Include default devise modules. Others available are:
|
224
|
-
|
225
|
-
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
226
|
-
|
227
|
-
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable, :timeoutable, :trackable
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
validates :username,
|
232
|
-
|
233
|
-
uniqueness: true,
|
234
|
-
|
235
|
-
length: { minimum: 3, maximum: 25 }
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
def to_param
|
240
|
-
|
241
|
-
return self.username
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
```
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
post.rb
|
254
|
-
|
255
|
-
```ruby
|
256
|
-
|
257
|
-
class Post < ApplicationRecord
|
258
|
-
|
259
|
-
belongs_to :users, optional: true
|
260
|
-
|
261
|
-
generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(20)
|
262
|
-
|
263
|
-
validates :title,
|
264
|
-
|
265
|
-
presence: { message: 'は空白にできません'},
|
266
|
-
|
267
|
-
length: { minimum: 5, maximum: 40 }
|
268
|
-
|
269
|
-
validates :content,
|
270
|
-
|
271
|
-
presence: true,
|
272
|
-
|
273
|
-
length: { minimum: 120 }
|
274
|
-
|
275
|
-
validates :user_id,
|
276
|
-
|
277
|
-
presence: true
|
278
|
-
|
279
|
-
end
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
```
|