質問編集履歴

1

jobcontroller、modelファイルを追加

2020/08/13 04:04

投稿

sk48801728
sk48801728

スコア12

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
 
42
42
 
43
- ```ここに言語名を入力
43
+ ```new.html.haml
44
44
 
45
45
  .products__main__name
46
46
 
@@ -54,6 +54,174 @@
54
54
 
55
55
 
56
56
 
57
+ ### 該当のソースコード
58
+
59
+
60
+
61
+ ```Jobcontroller
62
+
63
+ class JobsController < ApplicationController
64
+
65
+ before_action :authenticate_user!, except: [:index, :show]
66
+
67
+ before_action :set_job,only: [:show]
68
+
69
+ before_action :move_to_signin, except: [:index, :edit, :update]
70
+
71
+ before_action :limit_editer, only: [:edit, :update]
72
+
73
+
74
+
75
+ def index
76
+
77
+ @jobs = Job.all.order('id DESC').limit(10)
78
+
79
+ end
80
+
81
+
82
+
83
+ def new
84
+
85
+ @job = Job.new
86
+
87
+ @job.images.new
88
+
89
+ end
90
+
91
+
92
+
93
+ def create
94
+
95
+ @job = Job.new(job_params)
96
+
97
+ if @job.save
98
+
99
+ redirect_to root_path notice: "登録に成功しました"
100
+
101
+ else
102
+
103
+ @job.images.new
104
+
105
+ render :new
106
+
107
+ end
108
+
109
+ end
110
+
111
+
112
+
113
+ def edit
114
+
115
+ @job = Job.find(params[:id])
116
+
117
+ end
118
+
119
+
120
+
121
+ def update
122
+
123
+ @job = Job.find(params[:id])
124
+
125
+ if @job.update(job_update_params)
126
+
127
+ redirect_to root_path
128
+
129
+ else
130
+
131
+ render :edit
132
+
133
+ end
134
+
135
+ end
136
+
137
+
138
+
139
+ def destroy
140
+
141
+ end
142
+
143
+
144
+
145
+ private
146
+
147
+
148
+
149
+ def job_params
150
+
151
+ params.require(:job).permit(:occupation, :Posting_end_date, :employment_status_id, :job_description, :qualification, :salary, :treatment, :working_hours, :holiday, :work_location, :application_method, :contact, :location, :phone, :contact_name, :hp_address, :stop_id, :end_id, images_attributes: [:image]).merge(start_id: current_user.id)
152
+
153
+ end
154
+
155
+
156
+
157
+ def job_update_params
158
+
159
+ params.require(:job).permit(:occupation, :Posting_end_date, :employment_status_id, :job_description, :qualification, :salary, :treatment, :working_hours, :holiday, :work_location, :application_method, :contact, :location, :phone, :contact_name, :hp_address, :stop_id, :end_id, images_attributes:[:image, :_destroy, :id])
160
+
161
+ end
162
+
163
+
164
+
165
+ def move_to_signin
166
+
167
+ redirect_to '/users/sign_in' unless user_signed_in?
168
+
169
+ end
170
+
171
+
172
+
173
+ def limit_editer
174
+
175
+ unless Job.find(params[:id]).start_id.to_i == current_user.id
176
+
177
+ redirect_to root_path
178
+
179
+ end
180
+
181
+ end
182
+
183
+
184
+
185
+ end
186
+
187
+
188
+
189
+ ```
190
+
191
+
192
+
193
+ ### 該当のソースコード
194
+
195
+
196
+
197
+ ```employment_status_id.rb
198
+
199
+ class EmploymentStatusId < ActiveHash::Base
200
+
201
+ self.data = [
202
+
203
+ {id: 0, name: '選択してください'},
204
+
205
+ {id: 1, name: '正社員'}, {id: 2, name: '契約社員'}, {id: 3, name: '派遣社員'},
206
+
207
+ {id: 4, name: 'パート/アルバイト'}, {id: 5, name: '業務委託'}, {id: 6, name: '業務請負'}, {id: 7, name: '登録'}, {id: 8, name: '期間従業員'}, {id: 9, name: '紹介予定派遣'}, {id: 10, name: '準社員'}, {id: 11, name: '嘱託'}, {id: 12, name: '開業'}, {id: 13, name: '在宅/内職'}
208
+
209
+ ]
210
+
211
+ include ActiveHash::Associations
212
+
213
+ # has_many :jobs
214
+
215
+ end
216
+
217
+
218
+
219
+ ```
220
+
221
+
222
+
223
+
224
+
57
225
  ### 試したこと
58
226
 
59
227