Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Ruby on Rails helper method from a JavaScript js.erb file

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?

like image 909
Max Rose-Collins Avatar asked Jun 15 '26 22:06

Max Rose-Collins


1 Answers

Try this:

var content = '#{solve()}'
like image 110
user2503775 Avatar answered Jun 19 '26 12:06

user2503775



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!