0

I was under the impression that you could put javascript in a view template in Rails 3. For example, if I had this html in views/public/home.html.erb

<div id="block">click</div>

then in views/public/home.js.erb, I thought I could put the following javascript, and then click on html to trigger the javascript. However, when I tested it, I got no results. But if I put the javascript in assets/javascript/application.js, then everything worked fine...Shouldn't it also work if it was in a js template with the same name as the html view?

$(document).ready(function(){       

test(); 
}); 


function test() {

    $("#block").click(function() { 

         $('#block').hide();

       });

 }

3 Answers 3

2

Ummm. no. It just doesn't work that way. Only one of views/public/home.* will be rendered, depending on the responds type.

Sign up to request clarification or add additional context in comments.

2 Comments

but at the same time, for Ajax to work without a page refresh, doesn't javascript have to be in a view?
The javascript is included via assets, or as a <script> tag in the view being rendered... the ajax call chain is then in a second request. An ajax method calls the server, which responds appropriately depending on the call type - the ajax call may respond with a js.erb template, or json, or xml...
1

Javascript shouldn't be added as a view file (bla.js.erb). They must be put in assets/javascripts or at least in lib or vendor directory.

You must also require them in your application.js file, if you already don't use require_tree.

In this way you won't need to reference the javascript in any way in your view (the application.js will include it for you). Otherwise, you need to specify a layout to insert javascript files in block, because views are rendered after tag.

There are a lot of reason not to put javascript directly in html (except for tests obviusly), read the rails asset pipeline for more information.

When you create a view with a different extension from html.erb that will be used only if your url specify a format with that extension, for example mywebsite/users.json will return eventually a index.json.erb.

For AJAX you would like to return a JSON object, not javascript which is definitely not a correct approach. Remember that you are using a framework and you should follow it's guidelines. If you want to live it's rails, it will be hard to work with it.

4 Comments

o.k, thanks. Actually, I just checked a book I'm reading and they have a file called remove.js.erb in public/views/tags and there's a function called "remove" in the Tags controller. They do this apparently so the ajax will change the presentation without a page refresh...
having it in public/views/tags is different, that's not a view but just a place where you can put things always visibles. If you want really to call with ajax a javascript file which is a view, remember that you should append .js to your link if the view is myview.js.erb. For example: mywebsite/users.js
o.k., thanks for explaining. Very appreciative of any help you can give. Cheers.
No problem, I'm learning rails too and worked a bit with formats so I understood this part quite well. If you need any other help, just ask ;)
1

You can use javascript_include_tag

If you have the js files source1.js, source2.js in public/javascript then you can include them using

javascript_include_tag 'source1', 'source2'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.