August 9, 2007

Now You Can Add RSS Feeds To A Rails Site

RSS logoWe've recently added some RSS feeds to make it easier to keep track of changes made to the ProductCritic site. This is an easy feature to add extra value to your site and with Rails it's quite easy.

There a number of ways to do this but I decided to use a straight forward method of using an XML view.

First step is to create a new method in your controller. For the home page feed which provides updates on the latest product reviews on the site, I added an 'rss' method to the PageController class that looked something like this:
def rss
@products = Product.find(:all, :order => 'created_at desc', :limit => 20)
render :layout => false
end
Now all that is needed is to create a 'app/views/page/rss.rxml' file to create the RSS feed. In this example I used this:
xml.instruct! :xml, :version => '1.0', :encoding => 'utf-8'
xml.rss('version' => '2.0') do
xml.channel do
xml.title "Latest Products from ProductCritic"
xml.link(request.protocol + request.host_with_port + home_path)
xml.description("Recently added products to ProductCritic.")
xml.language "en"
xml.ttl "40"
xml.pubDate(Time.now.rfc822)
@products.each do |p|
xml.item do
url = request.protocol + request.host_with_port +
url_for(:controller => 'product', :action => 'show', :id => p)
xml.title("#{h(p.name)} (#{p.score})")
xml.description(h(truncate(p.teaser, 500)))
xml.link(url)
xml.guid(url)
end
end
end
end
Ruby and Rails both have some nice helper methods to make creating the feed. Ruby's Time class has a rfc822 method which correctly generates a date-time in the required format and Rails has a truncate method to keep the body of the post a reasonable length.

With this method, we've added feeds for:
  1. The main site - provides updates on the latest products added to ProductCritic
  2. Digital Camera Reviews - provides updates on the latest cameras added to the site
  3. Camcorder Reviews - provides updates on the latest camcorders added to the site
  4. Cell Phone Reviews - provides updates on the latest cell phones added to the site
Finally, we added a feed for every product that is on ProductCritic. This means that if you are interested in one specific product (e.g. the iPhone), you can get a RSS feed for it and get updates whenever we add a new review specifically for that product you're interested in.

If you have requests for any other feeds from the site send us some feedback and we'll see what we can do.

Labels: , ,

May 11, 2007

Making a Friendly Multiuser Rails Database Configuration Files

Many people and sites recommend not checking in your database.yml file directly in the subversion repository but instead check in a database.example.yml file that each developer is responsible for modifying themselves. The reason for this is because if other developers are working on the project at the same time you will both end up clobbering the file as one person changes the information to match their machine and you change yours back to match your machine.

Fortunately the database.yml (along with all the other rails config files) can contain code in them. This makes is possible to write this:

development:
adapter: postgresql
database: critic_development
username: <%= ENV['USER'] %>

test:
adapter: postgresql
database: critic_test
username: <%= ENV['USER'] %>

production:
adapter: postgresql
database: critic
username: deploy

For development and test configurations we use a local database that runs on our local development box with the username we logged in with but for the production server it runs as the 'deploy' user.

There is no need for passwords because the database servers are configured to not accept connections from remote sources.

For our small development team we find this solution works great and there is no need to special case our deployment to the production server by having a step to copy the production database.yml file. It may not work in some cases but for small development teams this might make your life a bit easier.

Labels: , ,

March 30, 2007

Top 6 Resources To Learn Ruby On Rails

In a previous post, we discussed why we are using Ruby On Rails to develop ProductCritic. We're not saying it's the best framework since sliced bread (nothing ever is) but it's fairly easy to learn if you're interested.

With Rail's growing user base, the resources for learning the framework are certainly growing. The following resources are what I am currently considering the best locations of information.

1) Free Original Screencasts

Three free movies that give a great overview and show what the framework can do. Watch them through at full speed to be entertained but then go through them and follow along with your own editor to rebuild the applications yourself.

2) Agile Web Development with Rails, 2nd Edition

When just starting out I found it useful to have the print book but it's also available in PDF as well. This book brings together all the components in building an application is really worth the money. It's been recently revised for 1.2 so it should have a reasonable shelf life.

3) The Ruby Way, 2nd Edition

This book provides a huge number of Ruby idioms and examples that I find really useful for learning the "ruby way" of completing tasks. I came from a C++/C# background and found a number of ruby semantics a bit strange at first, specifically blocks. Since the Ruby language allows for completing tasks in any number of ways I like this book to see what the current accepted methods are for certain tasks.

4) Jamis Buck's Blog

Jamis Buck is a core contributor to Rails and employee of 37signals. Out of all the blogs on the net about technical aspects of Rails I find his writing the most interesting and easiest to understand. He is also quite prolific churning out good content at regular intervals.

He also created the Capistrano deployment tool and provides a lot of insight into the tool and deployment in general which is useful.

5) PeepCode Screencasts

These screencasts are very high quality at a very good price. It's like having a veteran rails developer mentor you on a topic that would be rather hard to document in a book. I found the RESTful Rails and Test-First Development for Rails the ones the most useful so far but all of them have been very useful.

6) Programming Ruby, 2nd Edition

Known as the "Pick Axe" because of the cover image this is a great resource and reference for the Ruby Language. I find the Ruby Way more useful for learning how to do a specific task but if I want to understand all the details of the language I refer to this book.

This post brought to you by:
If you love to learn, have you ever considered earning your online degree? If you love learning, sign online and find out how easy it is to further your education with an online degree. Whether you are interested in business management, engineering, or medical billing, there is sure to be a career path you would like to choose!

Labels: , , , ,

March 23, 2007

Who Else Wants To Use PostgresSQL Instead Of MySQL?

In a previous post I mentioned that we use PostgreSQL as our database server. Common sense might indicate that we use MySQL as that is what the core team is using and what would probably be considered to be the defacto standard for Rails development.

So why are we using PostgreSQL?

Probably the biggest reason we use PostgresSQL is the simplest...I like it more than MySQL. This is one of the main reasons why we picked Rails as our development platform. When all things are equal, pick the one that is more enjoyable to use as you're going to be spending lots of time with it and it helps with motivation during those long hours of a startup.

Both databases are perfectly capable and I've used both on live sites but I've always found MySQL to be harder to use from the command line. This may be because I don't know how to use all the features but that's is part of the problem isn't it? Learning PostgreSQL on the command line was very easy and intuitive, MySQL isn't.

The other reason I like PostgreSQL over MySQL is that it's a completely free and open project. With MySQL it feels like it's a commercial project from a business that makes the code available (which it is). There is nothing wrong with this but I feel more comfortable knowing that PostgreSQL ownership is more distributed and that there is no large single point of failure in the project.

If you are picking based on technical issues you'll find a holy war that runs deep for many years about both projects. It seems to boil down that MySQL may be a bit faster but PostgreSQL is more reliable. With the latest revisions of each I'm not convinced that either is true. MySQL seems reliable enough for the millions of sites using it and PostgreSQL is fast enough for our site.

For ProductCritic, either database would have been fine so we picked the database that we find is more enjoyable to work with.

This post brought to you by:
Do you love computers? If you are looking for more information on Buying a brand New Computer, sign online. Computer technology changes constantly. If you want to stay on top of the Computer Operating Systems, do your research. If you are interested, you can check out the Computer based Buying Guide!

Labels: , , ,

February 26, 2007

Build A Website Using Ruby On Rails That You Can Be Proud Of

ProductCritic is developed with the Ruby on Rails framework. This entry will give some insight on how this came to be.

My experience with web apps started with ASP and later onto ASP.NET. I found ASP.NET a huge improvement over ASP but after working on a few sites I still found it lacking.

Specifically one thing ASP.NET does not have good support for is nice URLs. I'm sure there are ways to do it but I never found a good solution to removing those ugly .aspx page extensions. Also, the page/form centric model that ASP.NET uses is a bad design for medium to large sites I find. No matter how hard I try I've always found my ASP.NET based sites always becoming a bit of a mess if they take off in size. There were many other problems and things I just didn't like about ASP.NET so about a year and a half ago I began looking for an alternative.

I got interested in working with Ruby on Rails after seeing the original screencast. I'll never forget thinking "who is that crazy danish guy yelling 'whoops' every few minutes as he cranks out features on his demo app?" What impressed me the most was when I spent the time to duplicate what he was doing in the screencast and realized that this guy knew what he wanted when he built the framework. Pretty URLs? Yup. Organized directory structure? Yup. Support for automated tests? Yup. Database persistence layer without a huge amount of setup code needed? Yup. The feature list was just perfect except for one thing...I didn't know Ruby. I had heard of it before but never used it and coming from a C++/C# background it sure looked like Perl to me sometimes.

Luckily though, while Ruby can be as hard to read as Perl it doesn't have to be and if written well can be extremely easy to read. Once you understand blocks you'll be fine. I like to compare understanding blocks to understanding pointers in C/C++. The first time you see them they are odd but once you get the 'aha' everything starts to make sense.

After building a number of Rails sites and working with Ruby for more than a year I have become quite found of the language and the Rails framework had proven itself on a number of production sites in the past. So when it came time to pick a framework for ProductCritic there really wasn't any question that we would use Rails.

That said, there are a number of very good and sometimes overlooked features on Rails that are worthwhile to point out:
  • Leverage an experienced web developer. DHH and the Rails Core team have a lot more experience than I do in web development. Using there framework allows me to leverage their experience on my projects.
  • Deployment. Capistrano, the deployment tool that works with Rails is amazing for getting your site onto a production server in a safe reproducible manner.
  • Free and open. Why pay for something when an equal or better solution exists?
  • Fast enough. I've heard lots of stories of Ruby being slow but in all my tests and on all my sites it's never been an issue.
There are lots of web development frameworks that we could have used to build ProductCritic. We picked Ruby on Rails ultimately because I enjoy working with it. Picking what makes the development staff happy is ultimately the best deciding factor to use when all other things are equal. Happy developers are productive developers.

Labels: , , ,

February 9, 2007

Startup Lessons From ProductCritic - Why We Run Linux

This is part two of Startup Lessons from ProductCritic. Part One can be found here.

Deciding on the operating system that will host your application has a huge ripple down effect on the rest of your system. There is effectively two options you can choose. Windows or not-Windows which is effectively Linux but not always.

Choosing Windows will almost certainly lead to IIS, SQLServer, and C#/VB.NET and choosing Linux will almost certainly lead to Apache, MySQL/PostgreSQL, Perl/PHP/Python/Ruby. Yes, you can run ASP.NET under Mono and you can run Ruby on Rails under Windows but doing so I find is an uphill battle. If there is anything that I have learned is that working in the preferred environment makes life a lot easier.

The preferred environment means using the software in the same environment that it was built in. If you work in QA this is a bad idea because little bugs will go unnoticed but as a user it makes things far easier.

Luckily, both OS's have platforms for their preferred environment that are equally capable. There are different costs and skills associated with Windows and Linux but neither will prevent or enable you to be successful. Picking the OS is not a make or break decision.

This means that choosing the OS is probably going to come down to which is the preferred environment for the platform and probably even more likely which OS the developers like more.

For ProductCritic we used Ruby on Rails as the platform. Since Linux is the preferred environment for Rails, the OS decision was pretty much made for us, but there were a number of other reason why I think Linux is a superior platform for startup web sites:

* free and open, why buy an OS when one is free?
* reliable and lightweight, can run on small servers and slices, great uptime
* low cost Virtual Private Hosting is available

These are totally subjective and any strong opinionated Windows developer will come up with an equally favorable list for Windows. Hence the factor of developer preference. Ultimately we picked Linux because I like it more and that should make sense to anyone!

This post brought to you by:
No matter what type of computer you use, it's always important to stay up to date on the best computer Operating Systems. If you are in the market for a new computer, do you know what you want? Check out the online computer Buying Guide to see what computer will best suit your needs. No matter if you need a new computer, or just want Computer used Parts Online, the internet can help!

Labels: , , ,

January 4, 2007

HappyCodr - A Showcase Opportunity for ProductCritic

HappyCodr is a site that aims to be the main showcase for sites designed with Ruby on Rails. The guys at Start Us Up! have done a great job in providing a place for Rails developers to promote their hard work.

We're excited to have an opportunity to promote ProductCritic to the rest of the development community. HappyCodr's list of submitted sites is also a great place to see all the great work that other developers are doing.

Labels: , ,