Rails TypeError (can’t dump File)

For a while, I kept getting exceptions from my app in the form of “TypeError (can’t dump File):”. I finally found out that this was caused when I was using active_record_store with something like file_column, attachment_fu, or paperclip. Basically whenever you’re storing a file in session, that was too large for the session, you would experience this issue. Here is how to get around it:

Say you have a model like so (This is using file_column):

class Model < ActiveRecord::Base
  file_column :filename
end

Then in your controller you would want to add a line before you redirect off to clear that session:

class Controller < ApplicationController
  def create
    @model = Model.find(params[:model])
 
    @model.save!
    params[:model][:filename] = nil rescue nil   # Reset value here
    redirect_to models_path(@model)
  end
end

As you can see, I am resetting the filename value on the model. Now it shouldn’t complain that it can’t dump the file. Happy RAILSING!

How are website with Ruby on Rails built?

ruby on rails

RobbDogg asked:

I’ve gone throu the Sitepoint book “Build Your Own Ruby on Rails Web Applications” by Patrick Lenz, and have a good understanding of how web applications are built, but I don’t understand how an entire website is built using RoR. Is every page gernerated by one or more Ruby scripts or are their static HTML or RHTML pages that use a small amount of Ruby code to pull content from a database? I’m working on a site that uses PHP, but would like to convert it to RoR. Most of the pages use PHP to generate some content from infomation fetched from a database, and are fairly static except for the small part of the page that gets it content form the database. Would you use Ruby the same way you use PHP? Where would I put my website page files in the Ruby directory structure? under rails_root/public ?

Learn Ruby on Rails or Python?

ruby on rails

someoneoutthereishere asked:

I have some experience with C, and am looking to learn a language that can do some web based stuff. I’ve done a bit of Perl, but don’t really want to learn it. PHP is there also, but again, seems like it may be a bit outdated(?)I’ve heard a lot about Ruby on Rails, but from the description on their website it seems to mainly be used for database driven sites.

Python is also popular and seems to be learnable (for my level of experience).

My web host supports both, so I’m mostly just looking for a direction on which would be more useful to learn. The projects I would like to use it for would not be strictly database driven things (which as mentioned seem to be Ruby’s specialty), just general web things like um for example a simple calculator or form processor or things like that. Any suggestions, and if so, why? Thank you!

Help with Ruby on Rails?

If you come across something that stumps you with ruby or rails, or there is something that you don’t quite understand, let me know. I would love to help out with trying to solve your problem.

Also, if you have something you would like me to explain or create a “how-to” on, let me know about that too. Leave a comment here, or write me an email.
You can contact me at randy@freezzo.com

Rails Unit Test multi-array params

While writing some tests the other day, I came across a little bit of a stump. I have an action that required the use of a multi-dimensional param such as:

param[:user][:name]

This is exactly what I was doing, but you get the picture. I could have easily changed it to a single array, but that not the point. The solution in this example, would be to nest your hash in the test such as:

def test_should_do_something
  post :create, :some_object=>{
    :name=>'Bob'
  }, :user=>{ :name=>'Something' }
end

Full message for error_messages_for

I found this somewhere while looking around for the easiest way to provide my own full message for the rails error_messages_for output.

Basically what we are going to do here is provide a humanized string for a variable of the model, and when the error message is printed out, it will display that message. This give more control instead of just having “Email is required.”

class Person < ActiveRecord::Base
 
  HUMANIZED_ATTRIBUTES = {
    :name => "Please provide a name for this person.",
    :email => "You must specify an email address."
  }
 
  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end
 
  validates_presence_of :name,  :message=>''
  validates_presence_of :email, :message=>''
 
end

Override default find conditions for model

Here is a little trick I use when I want to override a find method for a model, instead of adding the conditions option to my association. While I don’t think you should avoid using the conditions options in your associations, this will provide an alternative:

class ModelName < ActiveRecord::Base
  def self.find(*args)
    with_scope(:find=>{ :conditions=>LIMIT_CONDITION }) do
      super(*args)
    end
  end
end

Basically what is happening, is that you are overriding the default find function for a model, and wrapping its own find method with a with_scope call. So now everytime you call Model.find(:all) or whatever options you want, it will execute it under that scope, with the conditions you specify.

Ruby random number by day

This is a cool little snippet of code that will return a random number, that changes by day:

def random_by_day(max_value)
    srand Time.now.strftime("%m%d%Y").to_f
    rand(max_value)
end

YouTube’s MP4 Video Formats

Based on the recent update to YouTube and the ability to request an mp4 version of the videos on there server, I was able to add the ability on Jamzee to download an iTunes file that you can import into iTunes. Now you can create and listen to your YouTube playlist, created on Jamzee, right on iTunes.

While I am not here to share the process to retrieve these files, you can get some great information here:

http://googlesystem.blogspot.com/2008/04/download-youtube-videos-as-mp4-files.html 

I am still in the process to figure out a nice way to allow those Jamzee playlists to be exported to an iPod. If anyone has any ideas about that, I would love to listen!

Rails Log Analyzer (RAWK)

Came across a sweet rails log analyzer. Doesn’t require that you use syslog or anything like the other log parsers that are out there. This one will work right on your development or production log files. Its called RAWK and you can find it here:

http://rubyforge.org/frs/?group_id=2517&release_id=15246