1

I am trying to put a nested form into one of my views. I have installed the nested_form Gem and performed the installation steps. The last thing I did was edit my app/view/layouts/application.html.erb file to include this line

<%= javascript_include_tag "nested_form" %>

But when I look at the rendered page, the javascript doesn't work. I examined the page source and saw that the rendered code for the nested_form javascript looks like this:

<script src="/assets/nested_form.js" type="text/javascript"></script>

So it appears to be looking in the wrong place. That .js file is located in public/javascripts, not in assets. Did I do something wrong or do I need to specify something else in that javascript_include_tag?

3
  • Are you running rails 3? Commented Oct 26, 2011 at 15:48
  • 1
    One assumes so, or he wouldn't be getting assets at all... Commented Oct 26, 2011 at 15:49
  • Sorry, I meant 3.1. Anyways, Ken answered it. :P Commented Oct 26, 2011 at 15:51

1 Answer 1

6

From rails 3.1 and onwards, javascript_include_tag will only look in the asset pipeline for javascripts. If you want to add public/javascripts into your asset pipeline, simply add the public path to your

Rails.application.config.assets.paths

array when you initialize your RoR application, since defines the paths the asset pipeline looks at.

So no you did not do anything wrong, the gem you are using is just outdated and it's for rails 3.0-. Although I recommend try moving the javascripts from the public folder to the assets folder and see if it works, because that is the preferred way to put javascripts in a 3.1 application.

Another way to solve your problem, though not recommended, is to disable the asset pipeline altogether, if you are not using it. This is done by removing

config.assets.enabled = true

in your config/application.rb

If you want to know more about the asset pipeline, Railscasts has a video on it http://railscasts.com/episodes/279-understanding-the-asset-pipeline

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

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.