What is therubyracer or libv8 for in a rails application?
Asked Answered
C

2

16

I was working on a project and was having issues with therubyracer and libv8 often, so i decided to remove them. It seemed to be the only thing that used them as a dependency was less-rails which I wanted to remove anyways.

My main question is what are they for and do I need them in the average application and if so why?

Cycloplegia answered 14/1, 2015 at 0:54 Comment(3)
They are generally used for the asset pipeline on platforms that don't have a JavaScript runtime installed by default. So if you do not compile assets on the target system or the system has a JavaScript runtime in the path they are not needed.Roomful
So a CentOS server that is not connected to the internet would or would not have this?Cycloplegia
If the server already has a JavaScript runtime such as node.js already installed it isn't needed. I don't know if centos comes with a JavaScript runtime also if you are not compiling assets on te box it isn't needed. Less.rb has a dependency on commonjs which appears to only use therubyracer or therubyrhino Check out the answer here for more info. #18688075Roomful
F
14

There are a few things for which a Rails app might use a Javascript Runtime:

1) The Rails Asset Pipeline requires a Javascript Runtime in order to perform Javascript compression.

2) Certain Rails ActionView Helpers like javascript_include_tag require a Javascript Runtime in order to execute Javascript functions.

Not every Rails application use those features, hence the reason that therubyracer gem is initially commented out in your Gemfile; Furthermore note that you could alternatively use NodeJS as your Javascript runtime.

For details, see:

http://guides.rubyonrails.org/asset_pipeline.html#javascript-compression

http://www.rubydoc.info/docs/rails/ActionView/Helpers/JavaScriptHelper

http://www.rubydoc.info/docs/rails/ActionView%2FHelpers%2FAssetTagHelper%3Ajavascript_include_tag

Proper "Rails" way to perform javascript on specific pages

Fathomless answered 30/6, 2016 at 18:57 Comment(0)
M
4

rubyracer provides following features:

 1. We can write your ruby codes inside js codes. 
 2. We can invoke the js functions from ruby codes.  
 3. Manipulation of javscript objects & the passing them to javascript functions.

Those are the few basic benefits of using rubyracer gem. Please read complete details here

Magnanimous answered 14/1, 2015 at 1:2 Comment(5)
item 1, does that have anything to do with js.erb files?Cycloplegia
when you are hitting your rails controller (via ajax) and want some interaction you need to your codes in your app/views/action_name.js.erb file. This file will hold your js codes + embedded ruby codes as well. :) Please check : richonrails.com/articles/basic-ajax-in-ruby-on-railsMagnanimous
In the provided link just search for app/views/products/_save.js.erb: That will clear your doubt :)Magnanimous
sounds terrible, why would we want to invoke ruby code form js or vice versa ?Homemade
OOPS!! I take my words back... I had to run JS inside Java (to port a template system to java github.com/nishants/jeyson, github.com/nishants/wirestub) ..If I ever need Sinatra/Rails port, will have to try the rubyracer...... too bad there is no pure ruby alternative like Nashorn for Java .Homemade

© 2022 - 2024 — McMap. All rights reserved.