Railsで/blog/:year/:month/:dayで日付の投稿を複数表示させたい(2)
(環境)
Rails4.2.2
# config/routes.rb
[sourcecode language="bash" collapse="false"]
get "/operations/:year:month:day" => "operations#list", :constraints => { :year => /[12][0-9]{3}/, :month => /[01][0-9]/, :day => /[0-3][0-9]/ }
[/sourcecode]
# app/controllers/operations_controller.rb
[sourcecode language="ruby"]
def list
@operations = Operation.all
d = Date.new(params[:year].to_i, params[:month].to_i ,params[:day].to_i)
@operations = @operations.where(opedate: d)
end
[/sourcecode]
# app/views/operations/list.html.erb
[sourcecode language="bash" collapse="false"]
<p id="notice"><%= notice %></p>
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th nowrap>入外</th>
<th>入院日</th>
<th>手術日</th>
<th width="5">定</th>
<th width="5">麻</th>
<th width="5">齢</th>
<th width="5">性</th>
<th>ID</th>
<th nowrap>患者氏名</th>
<th>眼</th>
<th nowrap>病名</th>
<th>術式</th>
<th nowrap>術者</th>
<th nowrap>助手</th>
<th nowrap>担当</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @operations.each do |operation| %>
<tr>
<td><%= operation.inout %></td>
<td>
<%#= operation.admissiondate %>
<% if operation.admissiondate.present? %>
<%= operation.admissiondate.strftime("%m/%d(#{%w(日 月 火 水 木 金 土)[operation.admissiondate.wday]})") %>
<%#= post.created_at.strftime("%Y/%m/%d(#{%w(日 月 火 水 木 金 土)[post.created_at.wday]})") %>
<% end %>
</td>
<td>
<%#= operation.opedate %>
<% if operation.opedate.present? %>
<%= operation.opedate.strftime("%Y/%m/%d(#{%w(日 月 火 水 木 金 土)[operation.opedate.wday]})") %>
<%#= post.created_at.strftime("%Y/%m/%d(#{%w(日 月 火 水 木 金 土)[post.created_at.wday]})") %>
<% end %>
</td>
<td><%= operation.special %></td>
<td><%= operation.anesthesia %></td>
<td>
<% begin %>
<%= (Date.today.strftime("%Y%m%d").to_i - operation.birthdate.strftime("%Y%m%d").to_i) / 10000 %>
<% rescue %>
<%= 200 %>
<% end %>
</td>
<td><%= operation.sex %></td>
<td><%= operation.patientID %></td>
<td><%= operation.patientname.truncate(6) %></td>
<td><%= operation.eye %></td>
<td nowrap><%= operation.disease1.truncate(8) %></td>
<td nowrap><%= operation.ope1.truncate(8) %></td>
<td><%= operation.operator %></td>
<td><%= operation.assist %></td>
<td><%= operation.inpatientdr %></td>
<td nowrap><%= link_to '編集', edit_operation_path(operation) %></td>
<td nowrap><%= link_to '削除', operation, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%#= link_to 'New Operation', new_operation_path %>
<%= button_tag type: 'button', onclick: "link_to('#{new_operation_path}')",
class: "btn btn-primary" do %>
<%= content_tag :span, "新規登録", class: "glyphicon glyphicon-plus" %>
<% end %>
[/sourcecode]
ブラウザで、
localhost:3000/operations/20160722
と入力すると、opedateが2016-07-22のリストが表示される。
ディスカッション
コメント一覧
まだ、コメントがありません