Rails 2.0 features: Multiple views

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.

Replay

Category: programming Time: 2007-10-26 Views: 1
Tags:

Related post

  • How can I get a views exposed filter to persist across multiple views? 2012-08-10

    I have 2 separate views that return a set of results for each of my content types. The fields are different, but the filters are exactly the same - Sort by Location / Distance. This Distance / Proximity filter is exposed, and it is set to 'remember'

  • multiple views want single search field is it possible in drupal? 2015-02-16

    I have developed Multiple views with multiple nodes.like(View1 , View2,View 3). In these three views Name field is same. so is that based on Name filed search feature is possible or not. I have tried with views default search criteria,but its working

  • TYPO3 Accessing multiple views in show view 2016-01-26

    For a project I built a custom products extension with multiple views (list, show, nav ... etc). The nav view is build right in the header and lists product categories, groups and the actual products. Now there a way too many sql queries every time t

  • How can I display custom post types under multiple views? 2012-03-15

    I want to create custom post types with multiple views based on URL. E.g., suppose I have a "Product" post type. I would like to have something like the following: http://example.com/products/product_name <-- Regular view of the product/regul

  • Pro/cons: Multiple lists or one big list with multiple views? 2012-07-05

    I have data that can go through different stages, during which it will have common fields and fields that are only used on specific stages. If I e.g. have these fields: CommenField1, CommenField2, StageA1, StageA2, StageB1, StageB2 They could either

  • When should I create multiple Displays of a View instead of multiple Views? 2012-08-01

    Is it simply a matter of convenience to create multiple Displays of a View instead of multiple Views, or is there a performance benefit? Does having any of the following items present in some Displays but not others affect the View as a whole? contex

  • Drupal 7 Views: Use one views.tpl.php file for multiple views 2012-12-22

    Views allows you to customize output with tpl.php files. My theme directory is getting overloaded with the .tpl.php files! Many of the files repeat the same content. I would like to use the same tpl.php file for multiple Views. How can I do this? Ide

  • Can I create Multiple forms assigned to multiple views for the same Sharepoint List? 2013-08-29

    I have a requirement where I need to render multiple views of the same list across my site collection, which is rather straightforward. However is it possible for me to create multiple forms (content types?) and assign them accordingly to the various

  • is it possible to have one views-view--VIEWNAME--page.tpl for multiple views? 2013-09-25

    I have 8 content types and 4 of them are the same and the difference is just their name. they all have a page view and the way I display them on screen is the same. instead of having 8 pages. (VIEWNAME_i stands for the i-th view.) views-view--VIEWNAM

  • How can I have ONE views-view-fields-VIEW_NAME--page.tpl.php for multiple views? 2013-09-25

    I have 8 content types and 4 of them are the same and the difference is just their name. they all have a page view and the way I display them on screen is the same. instead of having 8 pages. (VIEWNAME_i stands for the i-th view.) views-view--VIEWNAM

  • Multiple views to be displayed on a panels site 2013-11-08

    So as I've searched through drupal.stackexchange I see this question hasn't been fully answered, I guess it will be asked again and again until good solution is found I will try to summarize the problem and present found solutions. Imagine a site whi

  • Help with complex MVVM (multiple views) 2014-01-16

    I need help creating view models for the following scenario: Deep, hierarchical data Multiple views for the same set of data Each view is a single, dynamically-changing view, based on the active selection Depending on the value of a property, display

  • Delete Multiple Views 2014-01-17

    I currently delete a view by selecting it -> "Modify View" -> "Delete". I would like to delete 10 or so Views on the same List, does anyone know a way of deleting multiple views of a list all in one shot? No Sharepoint Designer,

  • Best practice for displaying multiple views in a single page 2014-05-25

    I am working on a Drupal 7 website where there are many pages which utilize multiple views. The way I am doing this right now is by creating a page from Drupal admin panel, and in the edit mode of that page I am calling views_embed_view to display th

  • Creating multiple views from Entityform Submissions 2014-07-02

    I would like to create multiple views containing filtered/ordered results from submissions made into forms created using EntityForm. In the documentation and in question on DA(The Entityform module allows me to create forms that any user can submit a

  • InfoPath 2013 Signature Line in multiple views 2015-07-31

    I have a form that can be digitally signed (signature type is XAdES-EPES), and form load rule to switch to another view once the form has been signed. In both view I have the same "Signature Line" control in use. That is, it was created in one v

  • Implementing multiple view models 2015-08-13

    I have the following scenario where I'm trying to use view models which will be accessed in the Visualforce page from various controller, in order to build the various UIs. interface IOne { void setOne(); } interface ITwo { void setTwo(); } public cl

  • MVVM with multiple view models that share a model 2015-09-01

    I have an application where there is an "inventor" model whose data I would like to display differently in different areas of the application. Therefore, I'd like to create different view models backed by that one model. For instance, in one sec

  • Multiple-View Query Optimisation 2015-09-15

    We have a monster view that contains joins to many (many) tables in order to return the data that is required. In some cases, to get a column of information, we've have to join onto 4 tables, just to get to the value we need (the database structure i

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.724 (s). 13 q(s)