🔥 Get 650+ Tech Books and Courses for $3/m for 3 months

Rails 2.0 features: Multiple views

    Myles Eftos
    Share

    The seed has been sewn for the next major release of the Ruby on Rails framework. Towards the end of last month, the Preview Release was announced and now that I have had a chance to play with it, I thought it timely to outline some of the new features.

    Multiple Views

    In version 1.2 of Rails, the respond_to block was introduced, which made serving up differerent data types, like XML or JSON really easy. All you needed to do was something like this:

    
    def index
        @stories = Story.find :all
        respond to { |format|
            format.html {}
            format.xml {
                render :xml => @stories.to_xml
            }
            format.json {
                render :json => @stories.to_json
            }
        }
    end
    

    Then, on the web browser, if you appended the file extension (eg /stories/index.xml) and you would get the content delivered in the requested format. You could even create your own custom types by adding MIME::Type.register to the bottom of your environment.rb file.

    One of the problems with this approach though, was there was no way to serve up different HTML pages based on the file extension. Because of the way the MIME::Type parser worked, adding another content handler with a mime type of text/html clobbered the default handler which meant the above code would serve up the wrong view.

    Enter Mime::Type.register_alias

    Now you can tell Rails to respond with HTML to as many file types as you like! Say you are designing a mobile version and an iPhone version of your site, you can create two new formats by dropping the following code in to the new /config/initializers/mime_types.rb file:

    
    Mime::Type.register_alias "text/html", :iphone
    Mime::Type.register_alias "text/html", :mobile
    

    This makes the following possible:

    
    def index
        @stories = Story.find :all
        respond to { |format|
            format.html {}
            format.xml {
                render :xml => @stories.to_xml
            }
            format.json {
                render :json => @stories.to_json
            }
            format.iphone {
               // Serve up the iPhone version
            }
            format.mobile {
               // Serve up the mobile version
            }
        }
    end
    

    Of course, having to manually render a different version in every respond_to block isn’t very DRY, so a new naming convention has been created for all of the view files. Rather than calling the view file in the example above index.rhtml, you can create three different versions based on the format that you are serving up, eg: index.html.erb, index.iphone.erb and index.mobile.erb.

    If rails finds a matching view it will serve that up, if not it will serve up the default .html.erb or .rhtml file. This makes serving up different versions of your site even easier.