実現したいこと
gemのカレンダーを使わずにオリジナルでカレンダーを実装しています。
文献を参考にしながらなんとかrubyでカレンダーを作りました。
がしかし、rubyで書いたコードをどうhtml.erbに反映させるかが分かりません。
一応.rubyで記述した配列のデータをhtml.erbには表示することまではできます。
お分かりの方いましたらご教授願います。
ruby
1class BookingController < ApplicationController 2 def top 3 @posts = [ 4 "今日からRailsの勉強するよー!", 5 "カレンダーページ作成中!" 6 ] 7 8 month_table = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 9 10 require "date" 11 today = Date.today 12 13 #うるう年か? 14 is_uruu = ->(year) { 15 (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 16 } 17 18 #西暦1年1月1日から何日目か(すべてグレゴリオ暦で計算) 19 days = ->(year, month, day) { 20 uruu = ->(y) { 21 y / 4 - y / 100 + y / 400 22 } 23 month_days = ->{ 24 month_table[0, month].inject(&:+) + (is_uruu.(year) && month > 2 ? 1 : 0) 25 } 26 y1 = year - 1 27 y1 * 365 + uruu.(y1) + month_days.() + day - 1 28 } 29 30 #曜日の計算 31 week_number = ->(year, month, day) { 32 (days.(year, month, day) + 1) % 7 33 } 34 35 #カレンダーの出力 36 Calender = ->(year, month) { 37 gen = ->(from, to) { 38 (from..to).map {|i| sprintf("%2d ", i)}.join 39 } 40 putout = ->(i) { 41 last = month_table[month] 42 last += 1 if is_uruu.(year) and month == 2 43 while i + 6 <= last 44 puts gen.(i, i + 6) 45 i += 7 46 end 47 st = gen.(i, last) 48 puts st unless st.empty? 49 } 50 puts "#{year}/#{month}".center(27) 51 puts "sun mon tue wed thu fri sat" 52 w = week_number.(year, month, 1) 53 puts " " * w + gen.(1, 7 - w) 54 putout.(8 - w) 55 } 56 end 57end 58
html
1<h1>Booking#top</h1> 2<p>Find me in app/views/booking/top.html.erb</p> 3<% @posts.each do |post| %> 4<%= post %> 5<% end %>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。