what's the point of having a jquery-rails - ruby-on-rails

So I installed Ruby on Rails(first time) and got down to starting a new project and a server. So as expected there was an error on running the Rails server command. Some gems needed to be installed, apparently. The bundle install command didn't work for some wierd ssl issue. So I decided to do it all
manually.
What strikes me as odd is that there's a gem for jQuery: jquery-rails. I don't understand the point of this, really. Why not simply download the jquery file and put it in public.
What's the point of having a gem here.
Is it simply convinience or is there another important reason behind it?

Jquery-rails does 2 things. First of all it bundles the appropriate version of jquery. I consider this only a convenience for jquery itself. For something like jquery-ui, jquery-ui-rails does a lot more: since jquery-ui is modular, that gem will serve to clients only the bits of jquery-ui you are using.
The other thing in jquery-rails is jquery-ujs. This is javascript that makes things such as passing the :remote => true option to form_for work. It used to be that rails itself contained a version of this for prototype, but with rails 3 this was extracted from rails to make it easier to use other javascript libraries than rails' previous default of prototype

I think this is a good idea, because it makes JQuery available as a versioned dependency.
If you need to update JQuery, you just have to change the version at one place in your app, and every page that needs it will use the new version.
And based on its homepage, there's also a test helper that you can use in your tests.

Related

Ruby on Rails Gem or Bower for CSS and Javascript?

In Ruby on Rails, which is better for CSS and Javascript dependencies?
To install them as Gem or as Bower dependency? Such as jQuery and or Bootstrap?
It is better to install CSS frameworks like Bootstrap as a Gem. Here is my favorite Bootstrap installation guide: https://launchschool.com/blog/integrating-rails-and-bootstrap-part-1
jQuery is already included in the default Rails setup.
This is very much a personal preference. There are schools of thought that go both ways.
I haven't looked in a long time, but oftentimes the gems are usually behind the current release of front-end libraries.
UI portability is greater if you keep the frontend/backend logic completely separate. If you wanted to switch to another backend tomorrow, how long would it take?
Using gems is faster, and you have a single tool (bundler) to handle the depedencies.
I have avoided providing my personal preference in favor of a neutral post.
AS my opinion ....
rather than gem/bower, you should download files and integrate to project
like for bootstrap download bootstrap files

What is therubyracer or libv8 for in a rails application?

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?
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
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

Rails 4 - Adding JS assets via gem

I am using a gem that somebody else wrote to serve the fabric javascript library. The gem is using an old version of the library (1.3) and I'd like to be using 1.4 (the latest version). I haven't found any other gems using this version. My question is, is this the best way to load assets, or is there a more preferred method? And, if so, how would I go about building this gem with the latest version of this library?
In my opinion, it is good to do so in most cases.
In your situation, depending on how much time you have, you may want to do one of the following:
1. Contribute to the gem
If the gem is open source, you may fork it, update to the newest version, and do a pull request.
By this way you also give contribution to the rails whole and the others who are facing the same problem as well.
Downside is this takes time. You have to wait for the author to accept the pull request and wait for the next version of the gem. But you can point your Gemfile to use your forked version until the new version is out ;)
2. Write your own gem
Writing a gem for rails providing assets is actually not difficult. You may follow other existing gem's structure and should be easy to understand.
A good example is https://github.com/rails/jquery-rails
Downside is you have to maintain the gem. Otherwise when fabric 1.5 is out, another one would ask the same question as yours again.
3. Put the assets in your vendor directory
Rails project by default do have a vendor directory. It's ok to put external assets here as well.
The above are my preferred way to manage external assets.

Bootstrap SASS Install

I am keen to use the new Bootstrap SASS however I have noticed that a precision value has to be added to continue. I am aware of this bug and how it benefits the project.
However I am unsure how to build this into my project, the Bootstrap Docs are pretty lacking here..
::Sass::Script::Number.precision = [10, ::Sass::Script::Number.precision].max
This was the code given above and link here - https://github.com/twbs/bootstrap-sass
I would note that I have already installed downloaded the gem and installed it using bower. Just need to get this code a place to call home!
Try putting the one-liner in application.rb
https://github.com/twbs/bootstrap-sass/issues/409

How to use Twitter bootstrap with rails 3.0

How am I supposed to use bootstap with rails 3.0 rather than >= 3.1 ? is there any plugin which supports rails 3.0 ?
I think all of the bootstrap gems require Rails 3.1 or greater. I recently had bootstrap on a 3.0.10 Rails app using the Less.js file that you download from their site: http://lesscss.org/. This is the simplest most basic way to use Twitter-Bootstrap; the file compiles all of your "my_file.less" files into css on the client side.
However, if you want to modify the variables (which is the real power of using this framework) than you need to compile it. You can take a look at this Less compiler: http://wearekiss.com/simpless. I've never tried that, but I hear good things about it and it works on Mac, Linux, or PC.
Probably the easiest thing to do - if you want to compile the code on server side - would be to upgrade your project to Rails 3.1.1 and just use one of the Twitter Bootstrap gems. This is actually exactly what I ended up doing. I was able to update my app to 3.1.1 and I used the Boostrap-Sass gem (just because I slightly prefer Sass).
If you decide to upgrade, follow this RailsCast: http://railscasts.com/episodes/282-upgrading-to-rails-3-1
It helped me a lot.
Ryan Bates also offers a video on how to incorporate Twitter Bootstrap into a Rails app: http://railscasts.com/episodes/328-twitter-bootstrap-basics.
Here's a link to the Sass version of Bootstrap that I am currently using: https://github.com/thomas-mcdonald/bootstrap-sass
Many solutions : you can upgrade to rails 3.1+, might be the better (not the easier, depending on you app) way. You can include the static files yourself if you don't intend to change anything that is handled at the less level. You can do it even if you intend to, but you'll have to recompile the files yourself (or find a way to automate it). Finally, there might be a gem out there that is compatible with rails pre-asset-pipeline, or an old version of a gem. You'll have to look for yourself if you absolutely want a gem.

Resources