Close and Go BackBack to Viget

Testing with Mock Objects in Rails

Ben Scofield
Ben Scofield, Development Director, November 30, 2006 0

UPDATE: May 24th, 2007
This post is now woefully out of date; since it was written, several excellent mocking libraries have been released, and some testing frameworks include mocking functionality of their own. Watch the Four Labs blog for an updated version of this, coming soon!

Media Temple Grid Server on Rails

Kevin McFadden
Kevin McFadden, Former Staffer, November 29, 2006 4

Earlier this week I wrote about LiteSpeed. Besides potentially being expensive, it also requires having your own server or VPS.

Another option, great for traffic-moderate sites, is Media Temple’s Grid Server (GS). The basic set up costs $20/month and there is an option to add a 64 MB Rails container. All users appear confined to a chrooted environment, which is great because other users have no way of accessing your files, especially those hard-coded database passwords. For additional money, you can allocate more memory to the Rails container: $25/month for 256 MB and $75/month for 1 GB.

Creating a GS account is pretty fast and easy. About thirty minutes after entering my contact and billing information, I received my GS login information. The following outline describes how to deploy your Rails application.


  • Create a new domain for your account, perhaps myapp.mydomain.com.

  • Enable the Rails container in the Media Temple control panel https://accountcenter.mediatemple.net/.

  • Run mtr generate_config to avoid having to enter the user and pass everytime. This info gets stored in a ~/.mtr file in clear text, but it should be secure enough since no one else may access your home dir and it has appropriate permissions.

  • The instructions for installing the necessary files into your container are found in the MT Grid Server guide.

  • Create your database in the control panel (Manage Databases, not MySQL or PostgreSQL Admin.)

  • We have a slightly custom rake process, but perhaps the standard rake db:migrate RAILS_ENV=production will work for you.

  • The permissions of your application files must must be readable by the web server:

    # cd into the rails container
    cd $HOME/../../containers/rails/

    # All files should be readable
    find . -type f -exec chmod a+r {} ;

    # All directories should be executable and readable
    find . -type d -exec chmod a+rx {} ;



  • Don’t forget to update your production settings for the database, ActionMailer, and other deployment specific features.

After this is done, you should be able to access your application. You can manage your container from the command line using Media Temple’s mtr command. Run mtr -h for more information.

In the long run, it may be more flexible and cost-effective to go the VPS route if you have, or want to learn, the Linux side of things; but, if that isn’t your thing, the Grid Server is a good deal.

References

Ruby on Rails: Making the Jump to LiteSpeed

Kevin McFadden
Kevin McFadden, Former Staffer, November 27, 2006 1

As RoR becomes more mainstream, hosting a Rails application in a production environment becomes increasingly important. There are numerous tutorials explaining how to cobble together mongrel, apache, lighttpd, fastcgi, and friends, but the amount of configuration can be daunting.

Why go to that much trouble when LiteSpeed “Just Works"™? For a basic “Hello World” application, performance compares very well to the cobbled together systems, and installing it takes less than five minutes. To summarize the screencast, here are the ten essential steps for implementing a name-based virtual host running on an Ubuntu server, although this will work on any OS supported by LiteSpeed:



  1. Grab a copy of LiteSpeed from http://www.litespeedtech.com/products/webserver/download/

  2. Install it. Answer N for PHP support and then go ahead and accept the defaults for the remaining questions.

  3. gem install ruby-lsapi, required for everything to work.

  4. If everything was successful, you can view the admin site on the port of your choosing.

  5. Under Server Admin -> Virtual Hosts, delete the Example virtual host.

  6. Under the Default Listener, delete the pre-configured virtual host listener.

  7. Under EasyRailsWithSuEXEC, add a new member virtual host.

    • Give your virtual host a name (e.g., myapp).

    • Specify the domain (e.g., viget.com.)

    • Enter the path to the top level of your rails application (i.e., where app and config are located.)

    • Save your changes.


  8. Instantiate the member virtual host to create an editable configuration.

  9. For the default listener, map the virtual host to the domain (myapp -> myapp.viget.com) and save.

  10. Following “Apply your changes” provides a handy button to restart the LiteSpeed service.

  11. Point your browser to one of the domains to verify it worked.

The instantiation part for step #7 is not necessary for one virtual host, but if you have more, step #8 requires a unique configuration for mapping purposes.

The downside is there are two versions of LiteSpeed: free as in beer and enterprise. As you know, anything with the word “enterprise” associated with it is generally expensive and LiteSpeed is no different. However, for most medium traffic sites the free version should work well, and if it is a high traffic site you can probably afford the cost or use multiple servers.

Punch it, Chewie!

References

Getting Started With RJS in Rails

Patrick Reagan
Patrick Reagan, Development Director, November 22, 2006 0 One of the features of Rails that makes it a great framework for building the next generation of "Web 2.0" applications is its tight integration with the script.aculo.us JavaScript library. What this means for your application is the ability to dynamically update the display based on the results of an action (in this case '/task/mark_complete'):
def mark_complete
  begin
    @task = Task.find(params[:id])
    @task.task_status_id = TaskStatus::COMPLETE
    @task.save
  rescue ActiveRecord::RecordNotFound
    render :nothing => true
    return
  end
  render_text @task.status.description
end

Adding this code in a view template is all we need to have our display automatically update:

   

Status: <%= @task.status.description %>

<%= link_to_remote 'Mark as Complete', :update =>, 'task-status-' + @task.id.to_s, :url => {:controller => 'task', :action => 'mark_complete', :id => @task %>

This works great for those times when we only need to update a single DOM element on a page. What about when we need to update multiple pieces of content on the same page? One solution that has worked quite well for us is the use of RJS templates in Rails.

Continue reading "Getting Started With RJS in Rails"

Tagging Text Automatically

Ben Scofield
Ben Scofield, Development Director, November 21, 2006 7

Classifications are important; we do better as users when we can use a well-structured classification scheme to find information we’re looking for. In the web 1.0 world, that played out in a hierarchy imposed by site designers (typified in the eternal sitemap) that the audience was forced to work with. Web 2.0 saw a move away from this predefined architecture and towards audience-defined taxonomies (sometimes called folksonomies) built on tags - end users associate tags with pieces of content, and use various mechanisms to navigate between similarly-tagged items.

The Problem

Tagging is a great strategy in certain circumstances, but it has a few important drawbacks. The one we’ll talk about here is the blank-state problem: if you’re relying entirely upon the audience to generate your tags, then new content in a system suffers an inherent disadvantage. When a browser comes to the site, they’ll explore the existing tag architecture, but they won’t find the new content (since it hasn’t been tagged yet). They may still be able to find it via some other mechanism (search, for example), but unless they then tag it the content will stay buried. It’s a rich-get-richer situation - well-tagged content will be found and tagged more often, while under-tagged content will not be found and will remain under-tagged.

Continue reading "Tagging Text Automatically"

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Tony,

I understand and agree that the back-end shouldn’t output code (html code), and only content. The templates (aka views) should do the trick, but instead of having lot’s of if/else conditionals inside the view, you may just output the following content.

No information available

The template would loop in an array and put all the <li>’s inside the <ul>.
I don’t see anything wrong, nor...