Ruby on Rails Tuesday, March 24, 2015

Hi Neeraj,

You most serious problem you are facing is that js files included in application.js are not rendered in the context of your request. They are meant to be precompiled (meaning when you deploy your application they are all compiled only once so that each request for them is very fast) and loaded separately.

There are a couple of ways around this, the easiest being adding a javascript block to your application.html.erb or bar.html.erb that renders that code instead. Another alternative would be to have your javascript call an API for that data.

If you aren't trying to render request-specific data there are other alternatives. Is that the case?


On Mon, Mar 23, 2015 at 7:34 PM, Neeraj Murarka <njmurarka@gmail.com> wrote:
Hello.

I am having some trouble accessing a method "foo" I defined in a controller. 

I was under the impression that if "foo" is in the controller that renders bar.html.erb, then bar.html.erb can in fact see a method in this controller provided I put the "helper_method: foo" notation in the controller. I was able to verify that foo was in fact accessible in the bar.html.erb file.

The problem is I want to access foo in alpha.js.erb. My application.js includes alpha.js (alpha.js.erb) implicitely because alpha.js.erb is in the app/assets/javascripts and application.js has "//= require_tree .". 

But alpha.js.erb cannot see foo. I have no idea how to get my custom ruby functions to become accessible in the js files. 

Would appreciate help. I scoured google for a few hours already and could not find much on this.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5a2078b6-c86e-4b39-b601-d242ceef804d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Daniel Evans

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAD2%3DUWVWGk%2BePy96w79eEQ1qMA%2BCY2StuJhGaiPzv%2B9meQ27Fg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment