Ruby on Rails Gem or Bower for CSS and Javascript? - ruby-on-rails

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

Related

What is the different between bower and gems for rails? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
as all we know bower has make all the things more easier to manage frontend files such as j-query,bootstrap and many others without downloading from the sources manualy. so,how about gems for rails? ruby on rails can require frontend and manage files like bower does.
I've just start to try using bower and newbie in front end development, I have a little bit knowledge about back end development especialy in ROR. I want to know how's bower works and what the advantage of it if we collaborate with ROR, we are currently developing web using ROR. we are usually install bootstrap and many others front end frameworks using gems, but we want to move and start using bower.
where is the different about bower and gems for rails? should I use bower to manage frontend files on rails development?
Well, the question is quite opinion-based, so I'll write my own as well. There are two main differences here. Firstly, bower as you laready mentioned is to handle frontend assets only - in that case anything installed with bower will not give any helper methods within your ruby code.
Example: font-awesome can be installed both ways, but gem will give you fa_icon view helper, which you won't have when you install it with bower.
On the other hand, not all assets are available in form of the gem and hence bower is giving you greater choise of stuff to install.
Personally, I prefer to use bower, unless I know there are some extreme advantages in using gem.
EDIT:
Apparently there is another alternative, which is a combination of gems and bower assets - rails-assets.org. It is a gem source wrapping bower components in a gem. So if you need to install angular, you can (apart from installing angular-rails gem) add this url as a source to your Gemfile and then install rails-resource-angular.
Since you already know what Bower is and how Rails works, I'm assuming you're asking what are the pros/cons of using one or the other for your front-end dependencies.
We can take something like Bootstrap as an example. The disadvantage to using a Rails gem, such as twitter-bootstrap-rails is that everytime the underlying asset (Bootstrap) is updated or changed, you must wait for the gem's authors to update the gem accordingly. If they are slow or the gem's development/maintenance is abandoned, then now you're stuck. The pro is that the authors have specifically adapted the asset to work with Rails and its implementation is usually well-documented and simple. The bootstrap gem for rails makes it pretty easy, you basically just delete sprockets directives and use SASS's import feature.
Bower, on the other hand, is not generally made with Rails in mind, and integrating whatever package my be a bit more involved. For example, if you use bower to get BS, just adding a SASS import (assuming you are grabbing the SASS version of BS) is not going to work because the SASS load path is not going to be set to look in your bower_components/bootstrap folder by default.
I think that if you want to incorporate bower in your Rails workflow, you really ought to take a look at the rails-assets gem. It makes the incorporation of bower much simpler. I would also take a look at this episode from GoRails about using rails-assets. The author, Chris Oliver, seems to have the opinion that this is the way to go.
Bower lets you upgrade/update web asset files like Bootstrap, jQuery etc. Bower is a program, while Gem is packaged code written for you that you can use in your Rails application.
If you want to compare Bower, you can try to compare it with Bundle. And the difference is that while Bower manages the version for web assets, Bundle is used to install, and update gems. For bundle to work successfully, you need to list the gems you want to install in your Gemfile.
For more information on Bower, you can read this article.

How to Include Angular in a Rails App

To include Angular in rails, I can think of three basic methods:
Use gem 'angularjs-rails' (which no one seems to do).
Download Angular, add it to /vendor and manually include it.
Use Bower to require it and then manually include.
I suppose the advantage of Bower is just the dependency model but apart from that, why would I pick any one of these over any of the other options?
A good approach is to use Rails Assets.
From their website:
Rails Assets is the frictionless proxy between Bundler and Bower.
It automatically converts the packaged components into gems that are
easily droppable into your asset pipeline and stay up to date.

Can an admin template be used in a Ruby on Rails web app?

I have been doing UI research and have come across admin templates at http://themeforest.net/. I was wondering how do you apply these onto a web app built on Rails. These templates look very similar to wordpress themes. Are they that easy to configure? Is it just as simple as setting up a link to the database to make the fields form capture data? I've been looking at this theme.
For admin templates I recommend using Active Admin. It's relatively easy to implement and gives you great admin screens with little effort.
Yes, You can. I'm trying to solve the same problem and so far I have a couple options:
1.) do it by hand, I've done this before, it works but takes a lot of time to truly understand how your theme is put together. First I would recommend using the included themes assets exactly as they are bundled with the theme. Don't assume that just because you have twitter-bootstrap-rails gem that the bootstrap classes in the theme will work. Link the assets statically and slowly extract out the static assets and replace them in the asset pipeline once you know they work.
2.) Use the strategy suggested in the install_theme gem (http://drnicwilliams.com/2009/10/06/install-any-html-themetemplate-into-your-rails-app/) the gem itself is not maintained any longer (i'm not sure about any forks), but the strategy is sound. Extract the core parts of the template into partials.
The short answer is yes, but there is no straight forward way to "import to rails"

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.

Create plugins or gems for Rails 3?

I have features I would like to be portable between my own Rails applications.
I wonder if I should create a gem or a plugin for each feature I want to be portable (sharable).
They are just for Rails (for now) because they include css, html, js and image files.
But I have been wondering, the things provided with plugins could be provided with gems too but not the opposite? So maybe it's better to learn how to create gems, because then you I don't have to learn how to create both gems and plugins? And gems seem to be more popular these days.
But then, from what I can understand one gem is shared between all rails app in the OS. So that means I can not customize it for each Rails app right? In that case, maybe creating a plugin is better cause it should be allowed to customize (editing css, js etc) each feature and to have it stored inside the Rails app itself and not in the OS level.
Some advices would be appreciated!
UPDATE:
So gem works great with css, html, js and image files? In a plugin I think you can have a MVC, your own models, views and controllers. Quoted from Rails guides "Storing models, views, controllers, helpers and even other plugins in your plugins". Is this possible too in a gem? Eg. I want to add a extension that gives me a nice Shopping cart (with own migrations, mvc, asset files) that will be hooked into the current Rails app. Is this possible as gem or only as plugin?
You're right that gems offer a little more than plugins. Versioning and dependencies on other gems being the main ones for me.
One gem needn't be shared across everything using ruby. You can install multiple versions of a single gem and specify in your environment.rb that a gem requires a specific version. E.g.
config.gem 'my-gem', :version => '1.2.3'
Also you can freeze gems into your rails application so that you know you are working with a specific version.
You might want to look at the jeweler gem which makes creating your own gems easier.
UPDATE
To include CSS, javascript etc I think you'll need to make an Rails engine which can then be bundled as a plugin or a gem. I've not done this but there's some coverage here and here.
The push with Rails 3 seems to be towards gems and away from plugins as a lot of support has been added to make gems work as well or better than plugins ever did. A well constructed gem is a great thing to have and share between different applications, and also reduces the amount of testing you will have to do since you can test the gem thoroughly before integration.
For extensions to Rails that use CSS, HTML and other assets, it might be that you need to build an engine to bundle this all up and allow it to fit neatly into an application.
As of Rails 4, plugins will no longer be supported.
Gems are the way forward.

Resources