スポンサーリンク

Railsで特定の曜日のリストを作成し、リンクを貼る

(環境)
Rails4.2.2

(1)モデル

rails generate scaffold operation message:string admissiondate:date opedate:date special:string anesthesia:string sex:string patientID:integer patientname:string patientkana:string birthdate:date admissionID:integer memo:text eye:string disease1:string disease2:string disease3:string disease4:string ope1:string ope2:string ope3:string ope4:string instrument:string instrumentmemo:string opeestimatetime:integer begintime:datetime operator:string assist:string inpatientdr:string operoom:string inout:string telephone:string desiredroom:string HBs:string HCV:string HIV:string Wa:string MRSA:string add01:string add02:string add03:string add04:string add05:string add06:string add07:string add08:string add09:string add10:string

(2)config/routes.rb

Rails.application.routes.draw do
  get 'static_pages/home'
  get 'static_pages/index'
  get 'static_pages/help'

  get 'operations/midori'
  
  get "/operations/:year:month:day" => "operations#list", :constraints => { :year => /[12][0-9]{3}/, :month => /[01][0-9]/, :day => /[0-3][0-9]/ }, :as => 'operations_list'
  
  resources :operations
 root 'operations#index'
end

routes.rb の上から記載した順番に判定されるので、「resources :operations」よりも上の行に、「get ‘operations/midori’」や、

「get “/operations/:year:month:day” => “operations#list”, :constraints => { :year => /[12][0-9]{3}/, :month => /[01][0-9]/, :day => /[0-3][0-9]/ }, :as => ‘operations_list’」

を記載する。このルーティングの記載方法に苦労した。

 

(3)app/controllers/operations_controller.rb (一部)

def index
  require 'date'
  #@operations = Operation.all
  #@operations = Operation.order("opedate")
  @q = Operation.ransack(params[:q])
  @operations = @q.result(distinct: true).order("opedate")
end

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

def midori
  @operations = Operation.all

  require 'date'
  base3 = Date.today
  @tuesdays   = (base3...base3.next_month).select{ |e| e.tuesday? }
  @wednesdays = (base3...base3.next_month).select{ |e| e.wednesday? }
  @fridays    = (base3...base3.next_month).select{ |e| e.friday? }
  @operation_weekdays = (base3...base3.next_month).select{ |e| e.tuesday? || e.wednesday? || e.friday? }
end

 

list では、localhost:3000/operations/20160722 というアドレスをブラウザに入力したときに、2016年7月22日の手術のリストが表示されるようにしている。

midori では、本日から3か月後までの火曜日、水曜日、金曜日のみの日付を、@operation_weekdays として取得している。

 

(4)app/views/operations/midori.html.erb (一部)

<h1>Operations#midori</h1>
<p>特定の曜日のリストを表示する</p>

<table class="table table-striped table-hover table-condensed">
  <thead>
    <tr>
      <th>日付</th>
      <th>手術件数</th>
      <th>曜日を表示</th>
      <th>最大件数</th>
      <th>満員または空き</th>
      <th>リンク</th>
    </tr>
  </thead>

  <tbody>
    
    <% @operation_weekdays.each do |e| %>
    <tr>
      <td><%= e.strftime("%Y/%m/%d(#{%w(日 月 火 水 木 金 土)[e.wday]})") %></td>
      <td><% c = Operation.where(opedate: e).count %><%= c %>件</td>
      <td><%= e.wday %></td>
      <td>
        <% case e.wday %>
        <% when 2 %>
          <% max = 22 %>
        <% when 3 %>
          <% max = 3 %>
        <% when 5 %>
          <% max = 22 %>
        <% else %>
          <% max = 0 %>
        <% end %>
        
        <%= max %>
      </td>
      <td>
        <% if c < max then %>
          <%= "空いています" %>
        <% else  %>
          <%= "満員です" %>
        <% end %>
      </td>
      <td>
          <% f = e.strftime("%Y%m%d") %>
          <%= link_to 'その日の手術リスト', operations_list_path(:year => f[0..3], :month => f[4..5], :day => f[6..7]) %>
      </td>
    </tr>
    <% end %>
    
  </tbody>
</table>

 

各日付のリンクを作成するのにも苦労した。

<% f = e.strftime(“%Y%m%d”) %>
<%= link_to ‘その日の手術リスト’, operations_list_path(:year => f[0..3], :month => f[4..5], :day => f[6..7]) %>

としたが、今のままだと、その日付の手術が1件もないと、エラーになってしまう。。。対策方法は、またあとで考える。

localhost:3000/operations/midori

image

 

(5)app/views/operations/listi.html.erb (一部)

<p id="notice"><%= notice %></p>

<% @operations.find(:first) do |operation| %>
<h1><%= operation.opedate.strftime("%Y/%m/%d(#{%w(日 月 火 水 木 金 土)[operation.opedate.wday]})") %>の手術予定</h1>
<% end %>

<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 %>

 

http://localhost:3000/operations/20160803 上記 midori のリンクから

image

スポンサーリンク

Rails

Posted by twosquirrel