teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

情報を追加いたしました。何卒宜しく御願い致します。

2020/02/29 01:45

投稿

raigakun
raigakun

スコア7

title CHANGED
File without changes
body CHANGED
@@ -102,4 +102,113 @@
102
102
  ###最後に
103
103
  必要な情報等ございましたら、ご教授頂けますと幸いです。
104
104
 
105
- 以上、お手数ですが、何卒ご教授の程宜しく御願い致します。
105
+ 以上、お手数ですが、何卒ご教授の程宜しく御願い致します。
106
+
107
+
108
+
109
+
110
+ #追記1
111
+ ###grop の場合と 個人の場合で createに渡ってくる情報が各々どうなのか、というデータ
112
+ 以下、task_controller内のcreateアクションにてbinding.pryを追記しました。
113
+ group_idの有無で条件分岐を試みてます。
114
+
115
+ ```task_controller.rb
116
+ class TaskController < ApplicationController
117
+
118
+ before_action :set_task, only: [:edit, :update, :destroy]
119
+
120
+ def index
121
+ @group = Group.find_by(id: params[:group_id])
122
+ end
123
+
124
+ def new
125
+ @group = Group.find_by(id: params[:group_id])
126
+ @task = Task.new
127
+ end
128
+
129
+ def create
130
+ binding.pry
131
+ if @group = Group.find_by(id: params[:group_id])
132
+ @task = @group.tasks.new(task_params)
133
+ else
134
+ @task = Task.new(task_params)
135
+ end
136
+ if @task.save
137
+ redirect_to group_task_index_path(@group)
138
+ else
139
+ render :new
140
+ end
141
+ end
142
+
143
+ def edit
144
+ @group = Group.find_by(id: params[:group_id])
145
+ end
146
+
147
+ def update
148
+ if @task.update_attributes(task_params)
149
+ redirect_to :root
150
+ else
151
+ render :edit
152
+ end
153
+ end
154
+
155
+ def destroy
156
+ @task.destroy
157
+ redirect_to :root
158
+ end
159
+
160
+
161
+
162
+ private
163
+ def task_params
164
+ params.require(:task).permit(:title).merge(user_id: current_user.id)
165
+ end
166
+
167
+ def set_task
168
+ @task = Task.find_by(id: params[:id])
169
+ end
170
+ end
171
+
172
+ ```
173
+
174
+ ###個人でタスクを新規作成
175
+ [実際のビュー](https://gyazo.com/90561c2dbb2bf0bb0b342be36395313b)
176
+ [binding.pry後、ターミナルの画面](https://gyazo.com/906c3a5db7fef992b1b3a936ec24965f)
177
+
178
+ ###グループでタスクを新規作成
179
+ [実際のビュー](https://gyazo.com/795b0c4eaf0f5ed6fdf58c6b0e18543f)
180
+ [binding.pry後、ターミナルの画面](https://gyazo.com/e04a20cd3e1bcf17656bcf94a9c15746)
181
+
182
+ ###タスクの新規作成のコード(個人とグループでviewが異なる様に、条件分岐を用いております。)
183
+ ```app/views/task/new.html.erb
184
+ <% - if @group.present?%>
185
+ <header class="header2">
186
+ <nav class="nav2">
187
+ <ul class="header_menu2">
188
+ <li class="nav-link">
189
+ <%= @group.name %>
190
+ </li>
191
+ <ul class="header_menu2_inner2">
192
+ <li><%= link_to "Create Group Tasks", new_group_task_path(@group), class: "nav-link listNew2", method: :get %></li>
193
+ <li><%= link_to "Edit Group",edit_group_path(@group), class: "nav-link listNew3", method: :get %></li>
194
+ </ul>
195
+ </ul>
196
+ </nav>
197
+ </header>
198
+ <div class="tasknewPage">
199
+ <div class="container">
200
+ <%= form_with model: @task, url: { action: :create }, class: "new_task", local: true do |f| %>
201
+ <%= f.label :title %>
202
+ <%= f.text_field :title, autofocus: true, class: "form-control taskName", placeholder: "タスク名" %>
203
+ <div class="text-center"><%= f.submit "作成", class: "submitBtn" %></div>
204
+ <% end %>
205
+
206
+ <% else %>
207
+ aaa
208
+ <%= form_with model: @tasks, url: { action: :create }, class: "new_task", local: true do |f| %>
209
+ <%= f.label :title %>
210
+ <%= f.text_field :title, autofocus: true, class: "form-control taskName", placeholder: "タスク名" %>
211
+ <div class="text-center"><%= f.submit "作成", class: "submitBtn" %></div>
212
+ <% end %>
213
+ <% end %>
214
+ ```