I've always worked in ERB and made heavy use of .js.erb files to render JS responses. I've recently fallen for Slim templates but I'm having a difficult time finding an equivalent to this that keeps files in the Slim format.
Assuming I'm executing the update action within my controller and the end looks like this:
respond_to do |format|
format.js {}
end
And let's pretend that I'm trying to just send alert('hello') as a response.
update.js works correctly and so does update.js.erb, naturally. If update.js.slim looks like this:
javascript:
alert('hello')
...it is sent as a response that looks like this:
<script type="text/javascript">(function() {
alert('hello');
}).call(this);
</script>
That won't work because the browser expects Javascript and that is an HTML file with script tags. Thanks, Slim. It DOES work if I use | to render plain text:
update.js.slim
|
alert('hi');
Everything after the pipe character shows up in SublimeText 2 color-coded and I still get access to Ruby code and variables within #{}. So far, this seems like my best and only option if I want to insist on keeping things in Slim format, but since it doesn't give me any of the benefits of Slim, I almost feel like just using .js.erb for these files might be better, since it has native support for this sort of thing.
Is there a better way of doing it?
Related
$('#some').append('<%= render 'my_partial' %>')
And it renders it as text. It always escapes html. I have tried in all ways to avoid escaping.
$('#some').append('<%= j render('my_partial') %>')
$('#some').append('<%= raw render('my_partial') %>')
Result is the same.
If you want your coffee (or any files) to be interpreted as ERB, then you need to append the file name with .erb ie app.coffee.erb.
But a better question is why? Doing so will tightly couple your coffee to both rails and ruby making re-use much harder - much like putting css inline in HTML.
There are two better options IMO:
Use data attributes in HTML and access them with javascript
If you only have a single variable to pass, try something like <%= javascript_tag "my_func(#{#my_var})" %>. This will invoke a javascript function with that variable.
I've upgraded a Rails 3 app to 4 and I'm trying to render some really simple partial thats not working correctly.
I'm using HAML and I have a layouts folder with a application.html.haml file inside. Within this file I call several partials that make up the template for the entire page. these partials reside in a application folder. For instance, I call:
= render "chromeframe"
which works perfectly. However, below this I have:
`= render "header"
which contains a lot of haml html code for the header of the page. My problem is this isn't getting rendered correctly and all I'm getting from that call is"
<header id="header">
<h1>Dummy</h1>
</header>
Still fairly new to Rails but I had this working perfectly in Rails 3 so I'm totally thrown by this problem. Any suggestions, I'm sure it's staring me in the face.
Thanks
Looks like you have a dummy _header.html.xxx or header.html.xxx anywhere in your views, that is found by render. It doesn't have to be a haml file.
Look at your deveopment.log. It should say Rendered ...header... (xx ms).
It tells you, where the partial is found.
Well since I am using a lot of helper methods in my view files and I avoid using html in most of my view files.
Example
myview.html.erb
<%=myhelper #myobject%>
so I end up using,the erb processing tags each time for each file.
<%=%>
I want to register .rb as a template handler or any other extention for that matter.
So my templates look like
myview.html.rb
myhelper #myobject
I am clueless on how to go ahead.
I found it,it seems railscasts already covered that part.
Its show notes,worth checking out.
https://github.com/railscasts/379-template-handlers/blob/master/store-after/config/initializers/ruby_template_handler.rb
Things without ruby are easy to read and render without those erb tags.
As the title, I want to abstract some function and share them between different unobtrusive javascript files. How can I do?
To be more specific, for example, I have two files: show.js.erb, create.js.erb in views/object/
When I response to ajax request, I will render them.
Now this two files share some same code, so I want to abstract them and put it to a new file.
How can I achieve this?
Like with html.erb files you can also have partial for js.erb files.
Just extract the common code into a partial (starting with a _, so e.g _common_code.js.erb in views/object/).
Then you can just use the render function in the show.js.erb or create.js.erb files like this to include your common code.
render "common_code"
try extract common logic to separate file, include it to layout and use common functions from *.js.erb files.
I'm a rails developer trying to learn Backbone and then I ran into this problem: since Underscore templates include symbols like <%=%>, I guess templates can't be included into erb files, so is it okay to have a rails partial for every single template? And what extension should it be?
You can escape the erb symbols by using two % in the opening tag, and put your backbone templates in the rails views:
<script type='text/template' id="my-template'>
<%%= name %>
</script>
will output the following in your page:
<script type='text/template' id="my-template'>
<%= name %>
</script>
Putting your Backbone templates directly in your rails views is IMHO the best option when you're trying to learn. You're already wrestling with the new concepts, no need to add another hurdle.
Starting with Rails 3.1, it provides two things that make working with Backbone templates a little easier: the asset pipeline, and automatic JST (JavaScript Template) compilation.
Create a directory in your app/assets folder called templates. This directory will automatically be picked up by the asset pipeline.
Next, name the files in that directory with an extension of jst and the type of template you are creating ejs (embedded javascript). You can even nest them in directories. For example:
app/assets/templates/my_template.jst.ejs
app/assets/templates/bookmarks/show.jst.ejs
The asset pipeline also allows you to use other templating languages like embedded coffeescript, mustache, handlebars, etc. by simply changing the file extension (and including any necessary gems).
Now to reference your JST templates in your Backbone views, simply use the path to the filename:
var Bookmark = Backbone.View.extend({
template: JST['bookmarks/show'],
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
You may need to add this line to your application.js:
// require_tree ../templates
Here's a nice article which explains all of this in a little more detail: http://www.bigjason.com/blog/precompiled-javascript-templates-rails-3-1
Where should you put your Backbone templates? I'd say nowhere. I believe that in most Rails applications, the server should be responsible for all rendering of HTML, while the client-side JavaScript should just be responsible for inserting that rendered HTML into the DOM. Among other things, this makes I18n easier.
The exception would be if Rails is simply being used as a lightweight backend for an application that runs mostly on the client side (though in that case, you might want to use Sinatra or something instead). In this case, Rails should probably render nothing, and have the JS do all the rendering.
Notice the underlying principle here. Either the server should be responsible for all rendering, or the client should. Splitting it will make life harder.