質問編集履歴

1

該当コードを全て載せました。誠に申し訳ございません。

2019/07/03 02:50

投稿

Aokichingo
Aokichingo

スコア10

test CHANGED
@@ -1 +1 @@
1
- Rails db:migrateが出来ないです
1
+ railsでcarrierwavewp使用して画像投稿を試みた表示されない
test CHANGED
@@ -1,31 +1,277 @@
1
- **以下のエラーが出ます。**
2
-
3
- ec2-user:~/environment/app $ rails db:migrate
4
-
5
- == 20190630053032 AddImageToUser: migrating ===================================
6
-
7
- -- add_column(:users, :image, :string)
8
-
9
- rails aborted!
10
-
11
- StandardError: An error has occurred, this and all later migrations canceled:
12
-
13
-
14
-
15
-
16
-
17
- **db/migrate/20190630053032_add_image_to_user.rb**
1
+ **20190703021554_add_image_to_post.rb**
2
+
18
-
3
+ ```ここに言語を入力
4
+
19
- class AddImageToUser < ActiveRecord::Migration[5.0]
5
+ class AddImageToPost < ActiveRecord::Migration[5.0]
20
6
 
21
7
  def change
22
8
 
23
- add_column :users, :image, :string
9
+ add_column :posts, :image, :string
24
-
10
+
25
- end
11
+ end
26
-
12
+
27
- end
13
+ end
14
+
28
-
15
+ ```
16
+
17
+
18
+
29
-
19
+ **post.rb**
20
+
30
-
21
+ ```ここに言語を入力
22
+
23
+ class Post < ActiveRecord::Base
24
+
25
+ mount_uploader :image, ImagesUploader
26
+
27
+ end
28
+
29
+ ```
30
+
31
+
32
+
33
+ **show.html.erb**
34
+
35
+ ```ここに言語を入力
36
+
37
+ <div class="post">
38
+
39
+ <h1><%= @post.title %></h1>
40
+
41
+ <div class="article">
42
+
43
+ <p><%= @post.description %></p>
44
+
45
+ <p><%= image_tag @post.image.to_s %></p>
46
+
47
+ <%= link_to 'トップへ',posts_path, class:"right" %>
48
+
49
+ <%= link_to '編集',edit_post_path, class:"right" %>
50
+
51
+ </div>
52
+
53
+ </div>
54
+
55
+ ```
56
+
57
+
58
+
59
+ **new.html.erb**
60
+
61
+ ```ここに言語を入力
62
+
63
+ <h1>新規投稿</h1>
64
+
65
+ <%= link_to 'トップへ',posts_path %>
66
+
67
+ <%= form_for @post do |f| %>
68
+
69
+ <p><%= f.text_field :title, placeholder: 'お名前',class:"form-control" %></p>
70
+
71
+ <p><%= f.text_area :description, placeholder: '内容',class:"form-control" %></p>
72
+
73
+ <%= f.file_field :image %>
74
+
75
+ <p><%= f.submit class:"btn btn-info" %></p>
76
+
77
+ <% end %>
78
+
79
+ ```
80
+
81
+ **post_controller.rb**
82
+
83
+ ```ここに言語を入力
84
+
85
+ class PostsController < ApplicationController
86
+
87
+ def index
88
+
89
+ @posts = Post.all.order(created_at:'desc')
90
+
91
+ end
92
+
93
+
94
+
95
+ def show
96
+
97
+ @post= Post.find(params[:id])
98
+
99
+ end
100
+
101
+
102
+
103
+ def new
104
+
105
+ @post = Post.new
106
+
107
+ end
108
+
109
+
110
+
111
+ def edit
112
+
113
+ @post = Post.find(params[:id])
114
+
115
+ end
116
+
117
+
118
+
119
+ def create
120
+
121
+ @post = Post.new(post_params)
122
+
123
+ @post.save
124
+
125
+ flash[:notice] = "タスク「#{@post.title}」を登録しました。"
126
+
127
+ redirect_to "/"
128
+
129
+ end
130
+
131
+
132
+
133
+ def update
134
+
135
+ @post = Post.find(params[:id])
136
+
137
+ @post.update!(post_params)
138
+
139
+ flash[:notice] = "タスク「#{@post.title}」を編集しました。"
140
+
141
+ redirect_to "/"
142
+
143
+ end
144
+
145
+
146
+
147
+ def destroy
148
+
149
+ @post = Post.find(params[:id])
150
+
151
+ @post.destroy
152
+
153
+ flash[:notice] = "タスク「#{@post.title}」を削除しました。"
154
+
155
+ redirect_to "/"
156
+
157
+ end
158
+
159
+
160
+
161
+ private
162
+
163
+ def post_params
164
+
165
+ params.require(:post).permit(:title,:description)
166
+
167
+ end
168
+
169
+ end
170
+
171
+ ```
172
+
173
+ **image_uploader.rb**
174
+
175
+ ```ここに言語を入力
176
+
31
- **rake db:migrate:resetを試しましたが rake db:migrateは出来ません。助けてください。**
177
+ class ImagesUploader < CarrierWave::Uploader::Base
178
+
179
+ # Include RMagick or MiniMagick support:
180
+
181
+ # include CarrierWave::RMagick
182
+
183
+ # include CarrierWave::MiniMagick
184
+
185
+
186
+
187
+ # Choose what kind of storage to use for this uploader:
188
+
189
+ storage :file
190
+
191
+ # storage :fog
192
+
193
+
194
+
195
+ # Override the directory where uploaded files will be stored.
196
+
197
+ # This is a sensible default for uploaders that are meant to be mounted:
198
+
199
+ def store_dir
200
+
201
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
202
+
203
+ end
204
+
205
+
206
+
207
+ # Provide a default URL as a default if there hasn't been a file uploaded:
208
+
209
+ # def default_url(*args)
210
+
211
+ # # For Rails 3.1+ asset pipeline compatibility:
212
+
213
+ # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
214
+
215
+ #
216
+
217
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
218
+
219
+ # end
220
+
221
+
222
+
223
+ # Process files as they are uploaded:
224
+
225
+ # process scale: [200, 300]
226
+
227
+ #
228
+
229
+ # def scale(width, height)
230
+
231
+ # # do something
232
+
233
+ # end
234
+
235
+
236
+
237
+ # Create different versions of your uploaded files:
238
+
239
+ # version :thumb do
240
+
241
+ # process resize_to_fit: [50, 50]
242
+
243
+ # end
244
+
245
+
246
+
247
+ # Add a white list of extensions which are allowed to be uploaded.
248
+
249
+ # For images you might use something like this:
250
+
251
+ # def extension_whitelist
252
+
253
+ # %w(jpg jpeg gif png)
254
+
255
+ # end
256
+
257
+
258
+
259
+ # Override the filename of the uploaded files:
260
+
261
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
262
+
263
+ # def filename
264
+
265
+ # "something.jpg" if original_filename
266
+
267
+ # end
268
+
269
+ end
270
+
271
+
272
+
273
+ ```
274
+
275
+
276
+
277
+ **以上がコードです。**