link_to_function Rails Ajax Reference
Link_to_function syntax for Haml template. Notice the “|” pipe which will allow for new lines in your code.
= link_to_function( "Add Line Item") |
{|page| page.insert_html :bottom, :invoice_line_items, |
:partial => "line_item", :locals=>{:line_item=>LineItem.new}} |
Build Your Own Calendar in Rails without any Plugins in less than 10 lines of Ruby Code
There are a number of terrific calendar plugins for Rails. But it’s almost as easy, if not easier, to implement your own calendar.
The steps are pretty simple. First get the @beginning_of_month and @end_of_month and iterate over the days in between. In the loop we check if the day matches the @beginning_of_month and if it does we get the offset ( 0 – 6 ) because if the month doesn’t start on a Sunday all our days won’t match up correctly. We print out the offset number as table cells <td class=’offset’></td>. We then need to consider that weeks ‘restart’ and restart our row if we’re at the beginning of the week… this is accomplished with </tr><tr>. Then we just output the date contained in table cells <td>#{d.day}</td>. The code is below and it’s pretty simple.
In this example I use haml, but you could just as easily use ERB (at the bottom). If you’re not familiar with haml, check it out at http://haml-lang.com/ Haml automatically handles wrapping your html so you don’t have to! The end result is that it cuts your html in half and makes it look pretty, which in turns makes it a lot more manageable long term.
#app/views/calendars/show.html.haml
%table#calendar
%tr
%th
Sunday
%th
Monday
%th
Tuesday
%th
Wednesday
%th
Thursday
%th
Friday
%th
Saturday
%tr
- @beginning_of_month = Date.civil(2009,12,1)
- @end_of_month = Date.civil(2009, 12, -1)
- (@beginning_of_month..@end_of_month).each do |d|
- if d == @beginning_of_month
- (d.wday).times do # offset beginning of calendar
< td class='offset'> </td>
-if d.wday == 0 #restart the week
</tr><tr>
== <td class='#{d}'> #{d.day} </td>
Here it is in ERB
<<span>table</span><span> id</span>=<span>'calendar'</span>>
<<span>tr</span>>
<<span>th</span>>Sunday</<span>th</span>><<span>th</span>>Monday</<span>th</span>>
<<span>th</span>>Tuesday</<span>th</span>><<span>th</span>>Wednesday</<span>th</span>>
<<span>th</span>>Thursday</<span>th</span>><<span>th</span>>Friday</<span>th</span>>
<<span>th</span>>Saturday</<span>th</span>>
</<span>tr</span>>
<<span>tr</span>>
<% @beginning_of_month = Date.civil(2009, 12, 1) %>
<% @end_of_month = Date.civil(2009, 12, -1) %>
<% (@beginning_of_month..@end_of_month).each do |d| %>
<% if d == @beginning_of_month %>
<% d.wday.times do %> <td class='offset'></td> <% end %>
<% end %>
<% if d.wday == 0 %> </tr><tr> <% end %>
<td> <%= d.day %> </td>
<% end %>
</tr>
</table>


