Topic: Confusion over helpers and content_tag blocks
Hello everyone, new rails developer wannabe here!
I am trying to create a calendar table to display the daily availability of every staff member of a company. What I think I need is a header with the days of the month and then a row (with as many cells as the number of the days) for each staff member. The different background color of each cell will represent a different availability status and the staff members change so I want all this to be dynamically generated.
After researching online and with Ryan Bates' Railscast #213 (Calendars Revised) as my guide, I have so far managed to generate the header of my table that displays the days of the month in each cell.
module ReceptionHelper
def reception(date = Date.today, &block)
Reception.new(self, date, block).table
end
class Reception < Struct.new(:view, :date, :callback)
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
daysHeader
end
end
def daysHeader
last_day = date.end_of_month.strftime("%d").to_i
(0..last_day).to_a.map { |day| content_tag :th, day }.join.html_safe
end
endNow, here is where the questions and confusion begins:
Removing &block and :callback takes away the { ... } functionality, as I found out. I can't really say I understand why.
Why does the above work but I can't use (..).each do and the content_tag block below?
(0..last_day).to_a.each do |day|
content_tag :th do
day
end
endIn my effort to display the rows for each staff, I first went with this:
def table
content_tag :table, class: "calendar" do
daysHeader + staffRows
end
end
def staffRows
staff.to_a.map { |staff| content_tag :tr, staff.name }.join.html_safe
endI added staff to the Reception class definition and I call with <%= reception @staff %> - if that's not a very good practice please feel free to shout at me
But instead of getting one row per staff member I get all the member names next to each other in one row and right above the header. Changing content_tag :tr to content_tag :td results in a cell with the name of each staff after the last header cell. Afterwards, I made some progress with this:
(0..last_day).to_a.each do |day|
content_tag :th do
day
end
endHowever this produces only one cell for each room while I want a row with as many cells as the header row.
As you understand, I am quite lost in this and it seems I am not even clearly getting why the part that works as I want it to is correct. I hope this post doesn't give the impression that I am looking to have everything served to me in a dish - I am rather looking for directions that will help me understand. ![]()
P.S: I have posted a question on StackOverflow if you want to answer there and get some points.
Last edited by sebkomianos (2013-02-21 08:05:49)