3

Is it possible to call a ruby helper method from within a js.erb file?

I have a helper in application_helper.rb called solve which makes a API call to a third party service, and they only have a ruby client.

The js.erb file isn't being run on the client side, it is being called from within a controller method and run server side as it is PhantomJS.

I call the JS file from the controller

Phantomjs.run('phantom.js.erb', url)

Then within the PhantomJS js.erb file I have tried

var response = '<%= solve(variable) %>'

which just sets the variable as the string <%= solve(variable) %>

I have also tried

var response = <%= solve(variable) %>

but that just seems to make the application hang and become unresponsive.

I have seen other questions similar to this. In those questions they are asking if it is possible to call it from client side JS which I know you need to use an ajax request to do so.

Is this possible?

Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45

3 Answers3

1

Try this:

var content = '#{solve()}'
user2503775
  • 4,267
  • 1
  • 23
  • 41
0

Need a bit more context for this question, but I'll try my best to answer:

  • Essentially, you wouldn't be able to access your application_helper methods outside of any .erb files. (ie. if you have application.js or any other js file in your pipeline and you are trying to <%= solve %> from there it wouldn't work - mainly because it isn't an .erb file)

There are a lot of ways and architecture to go about solving this, but here are two simple ones:

  • If you put the JS you want to evaluate inline on the same page as your partial/html.erb page by using <script> //JS ERB CODE GOES HERE </script> It will actually evaluate properly since it is inside of an erb file. However, this is generally looked upon as unclean...

What you probably want to do is pass the value (presumably) you want that comes from the "solve" application_helper in a 'data' attribute on the html element that it affects. By utilizing "unobtrusive javascript" in this way, you simply pass the value through markup and then in your JS you can get the variable by using jQuery code like this. Here's an example:

<%= link_to "test_link", root_path, :data => {:solve => solve } %>

Of course it doesn't have to be a link, any HTML element will do, and then in your jQuery:

$("#test_link").data("solve");

will return to you whatever output comes out of your "solve" method in the application_helper.

derekyau
  • 2,936
  • 1
  • 15
  • 14
  • Sorry, I should have said in my question. I want to call this helper method from a PhantomJS file. So it doesn't have anything to do with a partial/html.erb – Max Rose-Collins Sep 12 '13 at 08:33
0

it can possible but there are different ways to do it. one way is define the method in helper_method in your controller and call it from your view. and another way is use the gon gem to access some controller value in your javascript. please check what is best for you please check the below links for more help

http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method https://github.com/gazay/gon http://railscasts.com/episodes/324-passing-data-to-javascript

Sabyasachi Ghosh
  • 2,765
  • 22
  • 33
  • I'm not trying to call it from a view, and the js doesn't have anything to do with a view. It is all server side. I need to pass a var from the js file to a ruby method and then get the response. – Max Rose-Collins Sep 12 '13 at 08:56
  • i guess you can use the gon gem for this case. please check once though i have never tried Phantom js with gon – Sabyasachi Ghosh Sep 12 '13 at 09:01
  • Can you send a var to a ruby method from within JS and then get a response with gon? – Max Rose-Collins Sep 12 '13 at 09:04
  • you can set one variable in controller and you can access the variable in js using gon. please check the git documentation. – Sabyasachi Ghosh Sep 12 '13 at 09:06
  • This isn't helpful to me, I need to send a variable from the js file to a ruby method and then get a response. I can't set it in the controller first – Max Rose-Collins Sep 12 '13 at 09:09