回答編集履歴

2

追記

2016/02/21 20:58

投稿

退会済みユーザー
test CHANGED
@@ -42,102 +42,6 @@
42
42
 
43
43
 
44
44
 
45
-
46
-
47
- def edit
48
-
49
- @item = current_user.items.find_by(id: params[:id])
50
-
51
- end
52
-
53
-
54
-
55
-
56
-
57
- def create
58
-
59
- @item = current_user.items.build(item_params)
60
-
61
- respond_to do |format|
62
-
63
- if @item.save
64
-
65
- format.html { redirect_to items_continue_url }
66
-
67
- format.json { render :show, status: :created, location: @item }
68
-
69
- else
70
-
71
-
72
-
73
- @item_type = params[:item][:item_type]
74
-
75
- format.html { render :new }
76
-
77
- format.json { render json: @item.errors, status: :unprocessable_entity }
78
-
79
- end
80
-
81
- end
82
-
83
- end
84
-
85
-
86
-
87
-
88
-
89
- def update
90
-
91
- @item = current_user.items.find_by(id: params[:id])
92
-
93
- respond_to do |format|
94
-
95
- if @item.update(item_params)
96
-
97
- # 編集
98
-
99
- format.html { redirect_to @item, notice: '記事を編集しました。' }
100
-
101
- format.json { render :show, status: :ok, location: @item }
102
-
103
- else
104
-
105
- @item_type = params[:item][:item_type]
106
-
107
- format.html { render :edit }
108
-
109
- format.json { render json: @item.errors, status: :unprocessable_entity }
110
-
111
- end
112
-
113
- end
114
-
115
- end
116
-
117
-
118
-
119
-
120
-
121
- def destroy
122
-
123
- Item.transaction do
124
-
125
- @item = current_user.items.find_by(id: params[:id])
126
-
127
- @item.destroy
128
-
129
- respond_to do |format|
130
-
131
- format.html { redirect_to my_rubbish_profile_url(current_user), notice: '記事を削除しました。' }
132
-
133
- format.json { head :no_content }
134
-
135
- end
136
-
137
- end
138
-
139
- end
140
-
141
45
  ```
142
46
 
143
47
 

1

追記

2016/02/21 20:58

投稿

退会済みユーザー
test CHANGED
@@ -12,8 +12,6 @@
12
12
 
13
13
  通常はuser modelを作成し、news modelとアソシエーションを組む
14
14
 
15
- ※news modelって命名規則的に大丈夫なの?
16
-
17
15
 
18
16
 
19
17
  **newsを投稿する場合**
@@ -24,17 +22,121 @@
24
22
 
25
23
 
26
24
 
25
+
26
+
27
+
28
+
29
+ ※news modelって命名規則的に大丈夫なの?下記はnews modelをitem modelに置き換えたコード
30
+
27
31
  ```ruby
28
32
 
29
- #news controller
33
+ #item controller
30
-
31
- def new
32
-
33
- @news = current_user.items.build
34
-
35
- end
36
34
 
37
35
 
36
+
37
+ def new
38
+
39
+ @item = current_user.items.build
40
+
41
+ end
42
+
43
+
44
+
45
+
46
+
47
+ def edit
48
+
49
+ @item = current_user.items.find_by(id: params[:id])
50
+
51
+ end
52
+
53
+
54
+
55
+
56
+
57
+ def create
58
+
59
+ @item = current_user.items.build(item_params)
60
+
61
+ respond_to do |format|
62
+
63
+ if @item.save
64
+
65
+ format.html { redirect_to items_continue_url }
66
+
67
+ format.json { render :show, status: :created, location: @item }
68
+
69
+ else
70
+
71
+
72
+
73
+ @item_type = params[:item][:item_type]
74
+
75
+ format.html { render :new }
76
+
77
+ format.json { render json: @item.errors, status: :unprocessable_entity }
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+
86
+
87
+
88
+
89
+ def update
90
+
91
+ @item = current_user.items.find_by(id: params[:id])
92
+
93
+ respond_to do |format|
94
+
95
+ if @item.update(item_params)
96
+
97
+ # 編集
98
+
99
+ format.html { redirect_to @item, notice: '記事を編集しました。' }
100
+
101
+ format.json { render :show, status: :ok, location: @item }
102
+
103
+ else
104
+
105
+ @item_type = params[:item][:item_type]
106
+
107
+ format.html { render :edit }
108
+
109
+ format.json { render json: @item.errors, status: :unprocessable_entity }
110
+
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+
118
+
119
+
120
+
121
+ def destroy
122
+
123
+ Item.transaction do
124
+
125
+ @item = current_user.items.find_by(id: params[:id])
126
+
127
+ @item.destroy
128
+
129
+ respond_to do |format|
130
+
131
+ format.html { redirect_to my_rubbish_profile_url(current_user), notice: '記事を削除しました。' }
132
+
133
+ format.json { head :no_content }
134
+
135
+ end
136
+
137
+ end
138
+
139
+ end
38
140
 
39
141
  ```
40
142