1

I'm pretty much a novice to the world of Ruby on Rails, a few weeks back I followed Lynda's good ruby on rails guide so I kinda know how to set up a project etc. Anyway, I made an website in html/css but wanted to do it in rails after all so I went to work. I love how you can just "dump" files in folders and rails just picks them out but I also had a few lines of javascript code on my site to "activate" the jquery following. It was in my header

<script type="text/javascript" id="sourcecode">
    $(function(){

    $('.scroll-pane').jScrollPane({
    showArrows: false,
    autoReinitialise: true
        });

});
</script>

So the simple question now is, where do I put this code since rails doesn't really have an .

5 Answers 5

1

Check out this page for layout and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html

There is a section there about rendering your ERB templates. You need to put your javascript code in your .erb template, within the head, just like you would in a normal HTML page.

To be clear, you can save the contents of your script tag into a .js file and link to it in your template, just like you would in plain HTML.

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

Comments

1

You can place your JS:

  • in the erb template it deals with
  • under public/javscripts in a normal .js file that you can include in templates using the javascript_include_tag helper
  • in seperate .js.erb template files for ajaxian / manual include purposes

EDITED, per suggestion:

Personally, I'd choose one of the last 2 since those are more Convention over Configuration, or the "Rails" way, which is what I believe you're looking for.

7 Comments

Well, you may as well explain your last sentence. Voodoo programming comes out of statements like that.
Well I tried putting the code in a "jquery_slider.js" file in the public/javascript folder but it does not seem to help. So I am going to try your first option, but like Jordan said I would like to know what's bad about it.
See @Nick's response for the include tag that works as a test. But then change the include to <%= javascript_include_tag :name_of_js_file %>. The first choice I gave is bad because it's just like including javascript directly in HTML files. Combining HTML / javascript / css in the same file creates hard-to-manage projects.
@dmf85: I understand where you're going now. Certainly you would want to link out to an external file if this is supposed to be reused. I don't see anything like that specified in point #1 (meaning, it could just be a <script src="...">), so I was curious why you wouldn't just do that. For me, I would combine your point #1 and #2 and say that's the correct way of linking an external script.
I put the website up on Heroku so maybe you guys can take a look, morning-spring-991.heroku.com/home/index and this is the non rails site no-illusions.nl/keepmewaiting As you can see the non rails version has the blue scrollbar, all the js code is being loaded but it does not seem to work. I think it's because this code <script type="text/javascript" id="sourcecode"> $(function(){ $('.scroll-pane').jScrollPane({ showArrows: false, autoReinitialise: true }); }); </script type="text/javascript"> should be placed in the header instead of an JS file.
|
0

I put all scripts in public/javascripts in a directory structure that mirrors controller/action names + a vendor directory and a common directory. In my application.html.* I have code to load what I need for the controller/action into the header. I make a strong effort to not put javascript into my .haml (.erb) files but am willing to break that rule if I must.

Comments

0

I would put them in public/javascripts/ until Rails 3.1.

Also make sure you are including your javascripts in the template.

<% javascript_include_tag :all %>

2 Comments

Doesn't Rails automatically put all the js files from that folder in the header?
No. It will only if you add <% javascript_include_tag :all %> to the <head></head> block in the application template file.
0

For loading reasons, don't you want certain javascript snippets to be called after the template loads rather than in the beginning (ex: called in the header)?

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.