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

質問編集履歴

1

view,controllerのソースコード追記

2019/12/13 01:21

投稿

ktanizaw
ktanizaw

スコア5

title CHANGED
File without changes
body CHANGED
@@ -50,6 +50,129 @@
50
50
  end
51
51
  ```
52
52
 
53
+ ```ruby
54
+ class TasksController < ApplicationController
55
+ before_action :set_task, only: [:show, :edit, :update, :destroy]
56
+
57
+ # GET /tasks
58
+ def index
59
+ @tasks = Task.all
60
+ @title = Task.group(:title).pluck(:title).sort
61
+ @tasks = Task.order(created_at: :desc)
62
+ if params[:sort_expired]
63
+ @tasks = Task.all.order(deadline: :desc)
64
+ end
65
+ end
66
+
67
+ def search
68
+ @tasks = Task.where('title LIKE ?', "%#{params[:title]}%")
69
+ @title = Task.group(:title).pluck(:title).sort
70
+ render :index
71
+ end
72
+
73
+ # GET /tasks/1
74
+ def show
75
+ @task = Task.find(params[:id])
76
+ end
77
+
78
+ # GET /tasks/new
79
+ def new
80
+ @task = Task.new
81
+ end
82
+
83
+ # GET /tasks/1/edit
84
+ def edit
85
+ end
86
+
87
+ # POST /tasks
88
+ def create
89
+ @task = Task.new(task_params)
90
+
91
+ if @task.save
92
+ redirect_to @task, notice: 'Task was successfully created.'
93
+ else
94
+ render :new
95
+ end
96
+ end
97
+
98
+ # PATCH/PUT /tasks/1
99
+ def update
100
+ if @task.update(task_params)
101
+ redirect_to @task, notice: 'Task was successfully updated.'
102
+ else
103
+ render :edit
104
+ end
105
+ end
106
+
107
+ # DELETE /tasks/1
108
+ def destroy
109
+ @task.destroy
110
+ redirect_to tasks_url, notice: 'Task was successfully destroyed.'
111
+ end
112
+
113
+ private
114
+ # Use callbacks to share common setup or constraints between actions.
115
+ def set_task
116
+ @task = Task.find(params[:id])
117
+ end
118
+
119
+ # Only allow a trusted parameter "white list" through.
120
+ def task_params
121
+ params.require(:task).permit(:title, :content, :rank, :deadline, :status)
122
+ end
123
+ end
124
+ ```
125
+
126
+ ```ruby
127
+
128
+ <h1>Tasks</h1>
129
+
130
+ <div class="">
131
+ <p>タイトル</p>
132
+ <%= form_tag(tasks_path, :method => 'get' ) do %>
133
+ <%= text_field_tag(:title) %>
134
+ <p>ステータス</p>
135
+ <%= select_tag(:status) %>
136
+ <%= submit_tag '検索'%>
137
+ <% end %>
138
+ </div>
139
+
140
+ <table>
141
+ <thead>
142
+ <tr>
143
+ <th>Title
144
+ </th>
145
+ <th>Content</th>
146
+ <th>Rank</th>
147
+ <th>Deadline</th>
148
+ <th>状態</th>
149
+ <th colspan="3"></th>
150
+ </tr>
151
+ </thead>
152
+
153
+ <tbody>
154
+ <% @tasks.each do |task| %>
155
+ <tr>
156
+ <td><%= task.title %></td>
157
+ <td><%= task.content %></td>
158
+ <td><%= task.rank %></td>
159
+ <td><%= task.deadline %></td>
160
+ <td><%= task.status %></td>
161
+ <td><%= link_to 'Show', task %></td>
162
+ <td><%= link_to 'Edit', edit_task_path(task) %></td>
163
+ <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
164
+ </tr>
165
+ <% end %>
166
+ </tbody>
167
+ </table>
168
+
169
+ <%= link_to "終了期限でソートする", tasks_path(sort_expired: "true") %>
170
+ <br>
171
+
172
+ <%= link_to 'New Task', new_task_path %>
173
+ ```
174
+
175
+
53
176
  ### 試したこと
54
177
 
55
178
  FactoryBotではなくdescribe内に必要な値をcreateする記述に変更してみましたが関係ありませんでした。
@@ -81,4 +204,5 @@
81
204
  end
82
205
  ```
83
206
 
207
+
84
208
  よろしくおねがいいたします。