質問編集履歴

3

.

2018/07/10 06:59

投稿

nm3000623
nm3000623

スコア11

test CHANGED
File without changes
test CHANGED
@@ -175,3 +175,29 @@
175
175
  end
176
176
 
177
177
  ```
178
+
179
+
180
+
181
+ ```
182
+
183
+ class Challenge < ApplicationRecord
184
+
185
+ belongs_to :user
186
+
187
+ belongs_to :category
188
+
189
+
190
+
191
+ validates :user_id, presence: true
192
+
193
+ validates :content, presence: true, length: { maximum: 255 }
194
+
195
+ validates_presence_of :titile
196
+
197
+ validates_presence_of :body
198
+
199
+
200
+
201
+ end
202
+
203
+ ```

2

追加しました。

2018/07/10 06:59

投稿

nm3000623
nm3000623

スコア11

test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,33 @@
42
42
 
43
43
  ```ここに言語名を入力
44
44
 
45
+ class ChallengesController < ApplicationController
46
+
47
+ before_action :require_user_logged_in
48
+
49
+ before_action :correct_user, only: [:destroy]
50
+
51
+
52
+
53
+
54
+
55
+ def index
56
+
57
+ if logged_in?
58
+
59
+ @user = current_user
60
+
61
+ @challenge = current_user.challenges.build # form_for 用
62
+
63
+ @challenges = current_user.challenges.order('created_at DESC').page(params[:page])
64
+
65
+ end
66
+
67
+ end
68
+
69
+
70
+
45
- def create
71
+ def create
46
72
 
47
73
  @challenge = current_user.challenges.build(challenge_params)
48
74
 
@@ -64,9 +90,21 @@
64
90
 
65
91
  end
66
92
 
93
+
94
+
95
+ def destroy
96
+
97
+ @challenge.destroy
98
+
99
+ flash[:success] = 'メッセージを削除しました。'
100
+
101
+ redirect_back(fallback_location: root_path)
102
+
103
+ end
67
104
 
68
105
 
106
+
69
- private
107
+ private
70
108
 
71
109
 
72
110
 
@@ -89,6 +127,8 @@
89
127
  end
90
128
 
91
129
  end
130
+
131
+ end
92
132
 
93
133
  ```
94
134
 

1

1

2018/07/10 06:48

投稿

nm3000623
nm3000623

スコア11

test CHANGED
File without changes
test CHANGED
@@ -64,4 +64,74 @@
64
64
 
65
65
  end
66
66
 
67
+
68
+
69
+ private
70
+
71
+
72
+
73
+ def challenge_params
74
+
75
+ params.require(:challenge).permit(:titile, :description)
76
+
77
+ end
78
+
79
+
80
+
81
+ def correct_user
82
+
83
+ @challenge = current_user.challenges.find_by(id: params[:id])
84
+
85
+ unless @challenge
86
+
87
+ redirect_to root_url
88
+
89
+ end
90
+
91
+ end
92
+
67
93
  ```
94
+
95
+
96
+
97
+ ```
98
+
99
+ <%= form_for(@challenge) do |f| %>
100
+
101
+ <div class="form-group">
102
+
103
+ <%= f.text_area :titile, class: 'form-control', rows: 5 %>
104
+
105
+ </div>
106
+
107
+ <%= f.submit 'Post', class: 'btn btn-primary btn-block' %>
108
+
109
+ <% end %>
110
+
111
+ ```
112
+
113
+ ```
114
+
115
+ class CreateChallenges < ActiveRecord::Migration[5.0]
116
+
117
+ def change
118
+
119
+ create_table :challenges do |t|
120
+
121
+ t.string :titile
122
+
123
+ t.text :description
124
+
125
+ t.references :user, foreign_key: true
126
+
127
+
128
+
129
+ t.timestamps
130
+
131
+ end
132
+
133
+ end
134
+
135
+ end
136
+
137
+ ```