Which extension should I use for view templates in Rails? - ruby-on-rails

As far as I know, .rhtml is deprecated in Rails 2. In some guides there is only .erb, but both RubyMine and NetBeans IDE generate .html.erb and I've also seen it many people using.
I've tested both and they seem to work fine without any errors or warnings, but which one is correct? .erb or .html.erb

file_name.format.extension is the accepted convention now, where format is html, xml, json, anything really and extension is erb if you're using erb, haml if you're using haml and so on.

Typically you want to have the format in the file name.

Related

Rails 3.2+ best practice for using Haml to generate Javascript templates (JST)

I asked a specific question about problems I'm having with a specific gem intended to do this in a separate thread ( https://stackoverflow.com/q/18577033/1206117?sem=2 )
But I feel I may be on the "wrong boat" somehow because all of the questions I find about Rails/Haml/JST-templates are at least 2 years old, or go unanswered.
I'm writing an app with a lot of client-side JS and so want to use templates to render views (I'm using Backbone). I want to use Haml to write the templates.
I'm not looking for a debate about which gem/method is better, I'm looking for A WAY that works and has current support and active use. At present I cannot write my JS templates in Haml, and it's a bummer. I'm avoiding CoffeeScript at present since I'm still rather new to Javascript.
I've submitted an issue to the haml git repo.
https://github.com/haml/haml/issues/716
Looks like https://github.com/netzpirat/haml_coffee_assets gives you what you want. (window.JST templates, written in HAML, with inline coffescript support)

how to use ajax in rails3.1?

As we know, Rails 3.1 is using CoffeeScript and JQuery.
The old way that I handle the ajax request is respond the request to a template which name is some_action.js.erb
Now that we have CoffeeScript and I want my template could use coffeescript syntax.So what can I do?
Just changing the template file's suffix name?
some_file.js.coffee this works fine in the assets/javascript directory.
But I am handle the ajax request and its template is under the folder views,if just simply change the template name from some_action.js.erb to some_action.js.coffee.erb or some_action.js.coffee,it won't work,rails treat it like normal file and will not compile it from coffeescript syntax to normal javascript.
I am very sorry about my Enlgish ability,hope this time my question will explain my purpose.
Assuming you have upgraded to Rails 3.1, taking advantage of the new syntax is, like you suggested, as easy as converting your files over to CoffeeScript.
Now, you can't just name the file *.js.coffee and expect a conversion, but Ryan Bates at Railscasts recommends changing your files over manually as a great way of learning the syntax yourself. In fact, he has a screencast posted of him doing just that: http://railscasts.com/episodes/267-coffeescript-basics
Rails, then, will handle the compilation from js.coffee into .js for you.

Rails mixing templating languages

I'm working with a large project where the templates are written in erb. We're getting more and more into client side rendering of parts of the project and we have lots of exact duplicates in mustache of our erb templates.
I'd like to change the erb templates that are replicated in mustache to mustache and print the js templates out from those templates.
I haven't been able to figure out how to mix templating languages in rails though, is this possible? I'd rather not rewrite all the erb templates in mustache.
Thanks!
Maybe Isotope could be useful for you? I never tried it though.
I fear you'd have no direct solution...
An alternative:
You could reuse your good old html created by ruby if you insert in handlebars templates. But it's another js library...
http://www.handlebarsjs.com/

Can I render a haml template with Ruby on Rails metal?

I have metal working (thanks to Railscasts), but I have a haml template that I want to render instead of just plain text. Apparently you can render ERB , but I don't see any place to do the same with haml. Is it possible or will I just have to live with using the plain rails routing if I want to continue using haml?
Thanks!
Haml, like ERB, has a Ruby interface. However, also like ERB, using it without setting up the proper template caching (as Rails normally does for you) will make it very slow, almost certainly slower than Rails' routing. So be careful, and use #def_method rather than #render.

Is there an Xcode syntax colouring for Rails, Ruby, Erb? If not, how can I write one myself?

Xcode's syntax colouring is poor at best and textmate's looks great, but I like Xcode, since I program in C++, too. I'd like to keep everything in one place and take advantage of other Xcode features.
Has anyone already done this or is there an easy way to do it?
You can go to Editor>Syntax Coloring>Check HTML and do it again for Ruby
After some googling, it looks like there is indeed some support for ERB files in XCode. It's a little hidden, though.
Check this page out: ERB Highlighting. In brief, XCode is looking for .rhtml files instead of .erb files, so .erb files don't trigger the ERB syntax highlighting. The solution, then, is to go open
/Developer/Library/PrivateFrameworks/DevToolsCore.framework/Versions/A/Resources/Standard file types.pbfilespec
look for the mapping for rhtml:
Extensions = (shtml, jsp, rhtml);
And add erb to the list:
Extensions = (shtml, jsp, rhtml, erb);
Viola! Just restart XCode and you'll get erb syntax highlighting. There's probably a way to do it from within XCode itself, but I haven't found it yet.
Xcode can do custom syntax coloring, you need two files
pbfilespec: specifies MIME type, extension and some meta info
xclangspec: which holds identifiers etc. that need colouring
and put them somewhere in ~/Library/Application Support/..
I'm a Textmate user myself, so don't know if such a thing exists for ruby, and neither a more exact specification for those files, but examples for other languages can easily be found.

Resources