2

How does one embed ruby code in javascript for rails? I know you can do this easily in javascript responders for action methods (for e.g. in create.erb.js). But is there a way to do this for other custom javascript files that are included in the application using javascript_include_tag?

For e.g. in create.erb.js I can write code such as <%= if current_user_name ="xyz" %>

Is there a way to have this type of embedding in a custom js file such as custom.js that is used across the application and not just for generating a response for format.js.

Thanks,

Tabrez

1
  • I had a similar question open earlier but people pointed out that it was too open-ended. I made changes to the question to incorporate the feedback but it was closed out by then. So, please read the content before thinking that it is a duplicate. Commented Dec 28, 2011 at 21:27

1 Answer 1

7

You can just create an _application.js.erb file (name it whatever you wish) in the views/shared folder and then include it in your application's layout file.

<%= render '/shared/application.js' %>

Make sure you wrap it in script tags like normal and ensure that you use the DOM ready wrapper within that JS file like you normally would as well.

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.