Posts Tagged ‘Ruby on Rails’

How to setup TinyMCE for Ruby on Rails

UPDATE: the yui_editor from yahoo might be a better option. especially larsklevan’s yui_editor plugin for rails. it worked the first time, was way easier to setup and tinymce, and is still working great.

see new post on how to setup yui_editor

1. Install the plugin (instructions available on the rails wiki)

  • ruby script/plugin install http://secure.near-time.com/svn/plugins/trunk/tiny_mce/
  • rake tiny_mce:scripts:install
  • Put the following in your views:
  • Add it to whatever controller needs it:
    class MyController < ApplicationController
    uses_tiny_mce

And add this code in your view admin.rhtml or application.rhtml

<% # Include TinyMCE before other JS to avoid problems -%>
<%= javascript_include_tiny_mce_if_used %>
<%= tiny_mce if using_tiny_mce? %>

That’s actually it. It will work really nicely (tested on Rails 2.0.2 and ruby 1.8.6 on localhost)

Next, you’ll want to be able to add images inline style. Here’s a useful tutorial Part 1 and Part 2 for that.

Make sure foil XSS attacks MAKE A WHITELIST. Technoweenie is awesome.

Another helpful tutorial.


Keep in mind, you’ll need to restart your server.

Also, here’s a more custom setup for the uses_tiny_mce call in your controller. This is the setup I prefer.

uses_tiny_mce(:options => {:theme => 'advanced',
                             :browsers => %w{msie gecko},
                             :theme_advanced_toolbar_location => "top",
                             :theme_advanced_toolbar_align => "left",
                             :theme_advanced_resizing => true,
                             :theme_advanced_resize_horizontal => false,
                             :paste_auto_cleanup_on_paste => true,
                             :theme_advanced_buttons1 => %w{bold italic strikethrough separator justifyleft justifycenter justifyright indent outdent separator bullist numlist separator link unlink separator undo redo },
                             :theme_advanced_buttons2 => [],
                             :theme_advanced_buttons3 => [],
                             :plugins => %w{contextmenu paste}},
                :only => [:new, :edit, :show, :index])

UPDATE JULY 10TH, 2008
You might have some issues with remote_for forms with tinymce. To solve the problem do something like this:

<% form_remote_for([:admin, Snippet.new], :before => "tinyMCE.triggerSave(true,true)") do |f| %>

Better yet, take a look at this awesome tutorial for how to use tinyMCE with AJAX in Rails

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

  1. this guy has a solution
  2. then you need to do this

he changed to database session helpful forum answer link

Generate PDFs

To generate pdfs:

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.

fastercsv docs

fastercsv project page

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

taken from here

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

Sort by date - year, month, and day

Here is how to sort by date thanks to Ryan Bates

Also see episode 70