How are website with Ruby on Rails built?

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?

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!

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 every time you call Model.find(:all) or whatever options you want, it will execute it under that scope, with the conditions you specify.

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

%d bloggers like this: