11

Here is what I have in my view:

<%= javascript_include_tag "social_sharing.erb" %>

Then I have: assets/javascripts/social_sharing.erb.js which includes some ERB tags like <%= post.id %> ...

The first problem is I see this error message:

`Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( social_sharing.erb.js )` to `config/initializers/assets.rb` and restart your server

If I follow those directions, I then get the js file served, but none of the ERB tags have been evaluated, so I still see <%= post.id %> in the linked js file.

1
  • 2
    you fiel name should be js.erb And <%= javascript_include_tag "social_sharing" %> is enough. Commented Aug 25, 2015 at 18:33

2 Answers 2

15

The file should be named assets/javascripts/social_sharing.js.erb (with .erb on the end), otherwise the Rails asset pipeline won't know to process it and will serve it up as if it were plain JavaScript. The include tag should read <%= javascript_include_tag "social_sharing.js" %> rather than .erb, although the .js is optional.

Everything in the assets folder will get processed through the asset pipeline and can be easily pulled in with asset helper tags.

HOWEVER, javascript_include_tag and the asset pipeline are for truly static assets that will only be precompiled once and then the same file served with every relevant request. If you're trying to include dynamic content such as <%= post.id %> that will vary from request to request, you will probably want to move the .js.erb file to somewhere in the views directory and render it as a partial, similar to the first answer for this question:

<script>
    render partial: '[somewhere]/social_sharing', post: @post
</script>

The file will need to be renamed _social_sharing.js.erb as well.

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

5 Comments

Any idea how to get my rails variables to work in the js.erb? For example, post is nil
The first answer here might help, you may need to render the .js.erb as a partial inside a <script> tag and pass in @post as a local - I'll update my answer to reflect this
Thanks. I guess this isn't really an awesome unobtrusive way to do things, but it works for getting my js out of my view file for now.
It says "missing partial with name [somewhere]/social_sharing" , should it be .html.erb?
@htafoya Is the file in the views directory? [somewhere] should be a path in the views directory so that Rails knows where to look for the partial. For example, the full path to the file might be something like app/views/posts/_social_sharing.js.erb. The file should be .js.erb unless it contains HTML in addition to JavaScript code.
2

I'm not sure this is the answer, but what you're doing with your asset pipeline vis a vis the javascript looks unusual.

To include the js in your assets precompile add this line in your /config/initalizers/assets.rb file.

Rails.application.config.assets.precompile += %w( social_sharing.js )  

I think once you include this file in your asset pipeline compilation you do not need to use the javascript_include_tag at all.

If you try this, please let me if you still have the error and or the rendering problem.

4 Comments

Hi this is typically what I do, but I wanted to include it in a single page, sorry.
"include it in a single page" All assets in rails are compiled, even libs like jquery are compiled into application.js. You've got high rep in RoR (profile) so you know this stuff. So I guess, I'm not sure what you're going for.
Hey basically I was just trying to move a snippet of js from my html.erb file, but still have the instance variables available to the js.. I guess javascript_include_tag is more for static assets, etc.. So I was probably approaching things the wrong way to begin with. Your answer isn't wrong, so I will upvote you.
Thank you. Interesting question, upvoted, too. I also learned something from the accepted answer.

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.