Upgrading your views to Rails 2.0 15

Posted by brian Wednesday, July 11, 2007 20:18:00 GMT

For those who don't already know, in Rails 2.0, the .rhtml and .rxml file extensions will be deprecated in favor of the .erb and .builder extensions. This is being done to make a distinction between the MIME type of the file and the renderer. For example, what was once index.rhtml will now be index.html.erb, to indicate a MIME type of html, and a renderer of erb. Likewise, index.rxml might now be index.xml.builder. See the changeset for more information on why, if you're interested.

So, how about renaming all your rhtml view files in an existing project to comply with the new convention? My ghetto bash skills to the rescue. From the root of your Rails app:


myapp$ for old in `find app/views -name *.rhtml`; do svn mv $old `dirname $old`/`basename $old .rhtml`.html.erb; done

Of course, the world doesn't run on OS X and Linux alone. Luckily, our friend Ruby is cross-platform. So, alternatively:


c:\myapp> ruby -e "Dir[\"app/views/**/*.r*\"].each { |f| puts `svn mv #{f} #{$1}.html.erb` if f =~ /^(.+)\.rhtml$/ }"

One note — your app has to be under subversion source control for this to work. If not, you'll have to change the "svn mv" part to a simple "mv" or "copy" command, based on your platform.

And of course, you don't have to do this. The .rhtml extension will still work. But, of course, we want to be cool and stick with the conventions.

Update: Fixed a typo in the Ruby one-liner.

Update 2: Our example here only renames your .rhtml files to .html.erb. For a more useful approach, try this rake task (thanks Jamie), which takes care of rhtml, rxml, and rjs. Better still, you can use sake (thanks topfunky) and make a single command that will do the job across all your projects.

Comments

Leave a response

  1. Skeeterbug   July 11, 2007 @ 09:00 PM

    Makes me somewhat happy that I am doing DJango work now.

  2. topfunky   July 11, 2007 @ 09:54 PM

    That would make a nice sake task.

    http://errtheblog.com/post/6069

  3. AkitaOnRails   July 11, 2007 @ 11:08 PM

    Or a simple rake task that could be used to upgrade an old Rails app simple embedding this shell command and boon. Makes me somewhat happy that I am doing Rails work now :-)

  4. Jamie Hill   July 11, 2007 @ 11:25 PM

    Here is a rake task that will to the trick: http://thelucid.com/articles/2007/05/16/rails-edge-getting-your-view-extensions-ready-for-edge

  5. Luke Monahan   July 12, 2007 @ 12:01 AM

    Is there anything going to happen to rjs files? By this reasoning they should be:

    filename.js.rb
    

    Or not?

    L.

  6. Dr J   July 12, 2007 @ 02:18 AM

    nice little script. thx

  7. josh   July 12, 2007 @ 01:30 PM

    Luke: .rjs files are becoming .js.rjs AFAIK. RJS is its own little DSL, so it makes sense to name it that way.

  8. David Andrew Thompson   July 12, 2007 @ 08:20 PM

    ummm...YIKES!

    Yet another something-to-deprecate ticket item.

    Thanks for the great tutorial!

    I've linked back at my blog and posted my reflections.

  9. Sam Nardoni   July 12, 2007 @ 09:28 PM

    Nice little script, but shouldn't you be converting it to HAML ;)

  10. Brett   August 03, 2007 @ 05:38 AM

    Here is a sake script that includes all of the original plus the .haml => .html.haml conversion.

    sake -i http://pastie.caboo.se/84585.txt

  11. Kate Strickland   April 19, 2008 @ 04:35 AM

    platyfish unmammonized annamitic pedialgia manganesian mass devitalize allicient CuteShield IE Popup Blocker http://www.soleaconserve.it/eng/index.html

  12. Elroy Brown   April 21, 2008 @ 08:30 AM

    platyfish unmammonized annamitic pedialgia manganesian mass devitalize allicient Salisbury Diocese http://www.angelfire.com/on4/iowhospitalradio/

  13. kino   May 24, 2008 @ 01:12 AM

    Transcendental subjectivity, naturally, becomes modalized also in correlation with noematic descriptions.

  14. Ellroy   May 30, 2008 @ 11:19 PM

    As is proven in the ontological manuals, to avoid all misapprehension, it is necessary to explain that, in the full sense of these terms, our a priori concepts exclude the possibility of the phenomena, yet reason has lying before it the noumena.

  15. Jayne Farmer   November 13, 2008 @ 03:49 AM

    dt3lb29asi6bbjb5

Comment


(won't be published)