Why Does the Script Affect Everything on My Rails 3 App Even When Cased in This Code

Using Rails 3.1, where do you put your page specific JavaScript code?

I appreciate all the answers... and I don't think they are really getting at the problem. Some of them are about styling and don't seem to relate... and others just mention javascript_include_tag... which I know exists (obviously...) but it would appear that the Rails 3.1 way going forward is to wrap up all of your Javascript into 1 file rather than loading individual Javascript at the bottom of each page.

The best solution I can come up with is to wrap certain features in div tags with ids or classes. In the javascript code. Then you just check if the id or class is on the page, and if it is, you run the javascript code that is associated with it. This way if the dynamic element is not on the page, the javascript code doesn't run - even though it's been included in the massive application.js file packaged by Sprockets.

My above solution has the benefit that if a search box is included on 8 of the 100 pages, it will run on only those 8 pages. You also won't have to include the same code on 8 of the pages on the site. In fact, you'll never have to include manual script tags on your site anywhere ever again - except to maybe preload data.

I think this is the actual answer to my question.

Some assets not loading in an old Rails 3-2-stable app

The asset pipeline concatenates your assets to only generate and serve specified files, 'application.js', 'application.css' and images/fonts by default.

If you are explicitly including other css/js files in your application via the javascript_include_tag or stylesheet_link_tag helpers, you'll need to add those files manually to the pipeline via the config.assets.precompile += setting in your application.rb or an initializer.

Currently, those files should already be included within your application manifest via the require_tree . directive. So, the styles might be being applied right now too. If that's the case, you can just remove your code that adds the individual stylesheets separately to get rid of the 404s.

Still not sure how the app is working fine on heroku, maybe you have some heroku config that's enabling serving the static assets.

Single page Rails app

Having one page presented to the user doesn't necessarily effect how your structure that code ... you still need something to generate that page.

In your case, however, the display will be a list of posts. So you may not need a separate controller at all.

Assuming a rest-like structure, you would have:

Posts.index - list of posts
Posts.show - single post
Posts.create - create a post
etc
etc

So your index method becomes the "single page" and it can accept filtering parameters and display a list of posts accordingly.

Rails inline Javascript and Best Practices

Well if you really want to use best practices...Do not use inline javascript. Keep your HTML, CSS and Javascript clean and seperated from eachother. Ideally the html file should be usable without CSS and javascript.

The cleanest way, imo, is to just build your app using plain html/css, and enhance it with unobtrusive javascript to provide a better user experience.

A pattern for this is to include all your JS files at the bottom of your page, and start running your code with functionality like onDomReady.

Based on the comments I would like to add some possible options to get you started:

  • JQuery
  • YUI
  • Prototype


Related Topics



Leave a reply



Submit