Archive for May, 2008
Fixing rails Cookie Overflow error
It looks like the rails session only stores 4k of data. If you have a large form, it might not work.
Looks like I?ll probably have to change to the database session
Generate PDFs
To generate pdfs:
- Ryan?s Bates solutions, episode 79 (this works well, but is a little difficult to learn how to code in)
- http://rubyreports.org/ This looks promising for the future, but not much conversation going on about it.
- HTMLDoc This looks really GREAT. I?m used to html so the new level of coding to learn is much less. awesomeness. There is also this video tutorial.
- the list - this is a list of pretty much every pdf generator available and being used with rails
- expensive but awesome prince
Rails date manipulation / styling
To style dates in rails use strftime function.
For example: <%= comment.created_at.strftime(?%Y?) %>
Though, this might technically be ruby code not rails.
Importing CSV in rails with fastercsv
UPDATE: Here is updated code
Definitely, use fastercsv
The best way to explain this is with code. Here it is. Copy and paste. Copy and paste.
restful_authentication
Ryan Bates is the man and so is Rick Olson who created the plugin. Actually Mr. Olson is even more the man.
Exporting as csv in rails
UPDATE: I made some code changes that seemed to work better for my needs. Plus, it places everything in the respond_to .csv block. Check the pastie
Recipe 35 of rails recipes does not seem to work any longer on rails 2.0. I believe you have to now strictly use FasterCSV - which is supposed to be much better anyhow.
Here is a great tutorial on how to get it working
to install fastercsv, you must install the gem. Do the command: sudo gem install fastercsv
Date Range SQL clause in ruby
Here is how to do it:
Date range to SQL clause
(6.months.ago.to_date..1.year.ago.to_date).to_s(:db) => ?BETWEEN ?2005-07-27? AND ?2005-01-22??
Gruff graphs
The following are some good resource for how to use Gruff graphs:
The last one was HUGELY helpful:
def stats g = Gruff::Line.new(?580x210?) g.theme = { :colors => [?#ff6600?, ?#3bb000?, ?#1e90ff?, ?#efba00?, ?#0aaafd?], :marker_color => ?#aaa?, :background_colors => [?#eaeaea?, ?#fff?] }
g.hide_title = true
g.font = File.expand_path('path/to/font.ttf', RAILS_ROOT)
range = "created_at #{(12.months.ago.to_date..Date.today).to_s(:db)}"
@users = User.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
@votes = Vote.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
@bookmarks = Bookmark.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
# Take the union of all keys & convert into a hash {1 => "month", 2 => "month2"...}
# - This will be the x-axis.. representing the date range
months = (@users.keys | @votes.keys | @bookmarks.keys).sort
keys = Hash[*months.collect {|v| [months.index(v),v.to_s] }.flatten]
# Plot the data - insert 0's for missing keys
g.data("Users", keys.collect {|k,v| @users[v].nil? ? 0 : @users[v]})
g.data("Votes", keys.collect {|k,v| @votes[v].nil? ? 0 : @votes[v]})
g.data("Bookmarks", keys.collect {|k,v| @bookmarks[v].nil? ? 0 : @bookmarks[v]})
g.labels = keys
send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "site-stats.png")
end
