Posts Tagged ‘RoR’

9 Things You Should Know About Ruby on Rails

There are many programming languages, different frameworks, and uncountable developers. But Ruby on Rails has become one of the most popular languages among the developers. Though most of the people start their programming career with languages like Java, C++, PHP, etc., Ruby on Rails is what startups and some full-fledged companies use while developing websites. Image result for ruby on rails The first question that crosses everyone’s mind after hearing ‘Ruby on Rails’ is ‘What is Ruby on Rails?’. In this article, we are going to discuss some important things about ‘Ruby on Rails’. Let us start-

1. What is Ruby on Rails? Is it one thing or Ruby and Rails are two different things?

To be specific, Ruby is a programming language, whereas Rails is a web application framework designed to work with Ruby. Ruby is simple to learn and is super fun. It is easier than other programming languages. But the only problem with Ruby was that it was not designed for use on the web until Rails came. Rails are the most popular framework for Ruby basically make it possible to write a Ruby application and have it be a website.

2. Is ‘Rails’ good for the beginners?

Before moving to ‘Rails’, the first thing you need to learn is Ruby. The reason behind ‘Rails’ being good for beginners is because Ruby is simple to learn. Ruby is easier because the language is super flexible and not always case sensitive. And Rails is fun for beginners because it is robust and it does so much of the hard work for you. The developers do not have to bang their heads to find a minute mistake in the whole code. Basically, the first you have to learn Ruby and then you will be able to learn Rails.

3. How is Rails developer different from Ruby programmer?

As mentioned above, you have to learn Ruby first because Rails is a framework. Technically, a good developer can easily on Ruby web apps using other web frameworks like Sinatra. But a developer would not be hired just to write Ruby, So, it is important for a Ruby developer to learn Rails for future.

4. How good should a developer be to get hired as Ruby on Rails professional? What makes Rails so special?

To be honest, it takes a lot of time to become a Ruby on Rails expert. Basically, you should be comfortable in working on Ruby. You cannot dive into Rails without learning Ruby, but it is important to learn Rails afterwards. It is just like learning to ride a bicycle before learning to ride a bike. Specifically, you need to know about all the basics including data types, object orientation, debugging, etc. And the thing that makes Rails so special is because of its rapid prototyping. You can develop a Rails app with few hours. There are few frameworks that could make it possible.

5. What could be built with it?

Well, the answer is you can build any web application with Rails. Some of the most popular website built on Ruby on Rails are Shopify, Airbnb, Bloomberg, SoundCloud, etc.

6. Is possible to build mobile apps with it?

This is a bit complicated. It is not possible to literally build a mobile app in Rails. But you can build a web application in Rails, and use it as the backend for a mobile app. There are other options like RubyMotion which makes it easy to build native iOS and Android apps using Ruby. Talking in a straightforward manner, you cannot literally use Rails in developing mobile apps, but it could be an important part of the project.

7. Shouldn’t I learn JavaScript instead?

Well, if you are asking instead, then the answer is no. And if you ask also, then the answer is yes. The reality is a Ruby on Rails developer to need to learn JavaScript one time or another. It is not a prerequisite skill but it will help in future for further progress. And if you are thinking about which one you should be learning first, then the answer is Ruby is easier to learn than JavaScript. JavaScript is the first preference of students, but in the end, you have to learn both.

8. How long would it take to learn?

A few months of dedicated study can make you comfortable with Ruby on Rails. But if you want to be a master, you have to give at least 10,000 hours. Your level of familiarity with different programming languages would have an impact on you being comfortable with Ruby on Rails.
Also Read:Java vs JavaScript- All You Should Know

9. How to get started with this?

If you are already familiar with programming languages, then it would be easy for you to learn Ruby on Rails. But if you don’t know even a single thing about coding, then first you need to learn HTML & CSS. And then you need to learn Ruby, Git & command line.
The most important thing you need to do while learning something is having a project by your side. It would help you to learn, and find out your weak spots. These are the most common questions that come to everyone’s mind. So, if you have any question, just comment below and we will try to answer it!

Ruby on Rails vs Java Enterprise, Which one is Better?

Which is the best framework for your website? Have you ever given this thing a thought? Well, there are many languages which helps to design the framework for a website. There is Ruby on Rails, JEE, ASP.NET, Django and much more. But there are few languages that most of the programmers prefer. ASP.NET is a Microsoft platform which takes it straight to the risk zone. You cannot risk being locked into a Microsoft platform whereas other platforms are niche. So everything comes down to Java Enterprise (JEE) and Ruby on Rails (RoR).

Here are some differences through which you can figure out which one is better for your website:

#1 The focus of Ruby on Rails is on fast and agile development whereas Java Enterprise focus on flexibility and integration with enterprise IT.
#2 The common tasks in RoR are made very simple as compared to JEE. In JEE, even the simplest jobs need extraordinary efforts like to write XML configuration or auto-generate boilerplate code.
#3 RoR is used by start-up employees; JEE is used in the IT nerve-centres of investment banks. Image result for java vs ruby on rails #4 As a developer, RoR is pretty easy to learn, while the main problem a newcomer faces in JEE is to choose between huge number of different libraries and different ways to do the same thing. There is no particular libraries which are widely used.
#5 There are few people who work on Ruby on Rails, while every computer science student learns Java.
#6 In manageability of the code, JEE is better as it is easier to find out the things. Image result for java vs ruby on rails #7 Fortunately, both frameworks offer reasonable support for automated regression testing; Ruby on Rails probably a bit more so, because it relies primarily on automated tests (rather than a type system) to ensure things don’t fall apart horribly.

There are many aspects on which you can compare these two. Now the choice is yours. What do you think, which one would be the best for you? Tell us in comments!


Using Localtunnel in Rails Application

Localtunnel, as the name indicates, allows you to share your local web server on the internet. This kind of technique is very useful in cases such as:-
  • You want to demonstrate your web applications to clients.
  • Testing the web applications for compatibility issues against various operating systems and web browsers
  • Testing your web applications for compatibility in various mobile devices.
It is very easy to use. Your web application will be shared on public internet in a couple of seconds while it is running on your local system. Lets go through a step by step procedure to tunnel our very own RoR apps: STEP 1: Firstly, you need to install the client library for Localtunnel. So just install it by installing the ‘localtunnel’ gem or write it in your Gemfile:-
gem 'localtunnel'
and run ‘bundle install’ STEP 2: Run your local web server on any port! Let’s say you’re running on port 8080.
$rails s -p 8080
STEP 3: Now run localtunnel by specifying it the on port to be shared as:-
$ localtunnel 8080
It will establish a connection between your local server running at 8080 and localtunnel.com. Note that for first time you run the localtunnel, it needs you to specify your ssh public key for authentication. Here’s an example:
$ localtunnel -k ~/.ssh/id_rsa.pub 8080
After running the above commands, you will see something like this: – Port 8080 is now publicly accessible from http://xyz.localtunnel.com … Enjoy Tunnelling your apps !!

Bundle: Command Not Found: Heroku

Recently, I had an application where we had to use ruby 1.8.7 instead of default ruby on heroku cedar stack(1.9.x) so what we did was following:
  • Create a Heroku instance and deployed our application on the instance.
  • After deploying we changed the ruby version to 1.8.7 in Gemfile and deployed the application back.
  • Results: A dreaded “Bundle: Command not found” error.
We tried installing bundler and many other things but the error wont go away. We requested for a heroku support at that time and they explained that when you change ruby version after the deployment, heroku yet does not changes the gem path so we have to manually set the config using following commands:
heroku config:add GEM_PATH=vendor/bundle/1.8
We tried the solution and this worked for us. The Heroku team however explained that in case you use a custom ruby in a stack then we need to spin up an instance and then add the ruby directive at the time of deploying the application otherwise the gem path would not be configured. I hope this saves time for someone !!

Resources Controller for RoR Applications

Rails has one basic and most important principle to keep its code DRY (Don’t Repeat Yourself) which means writing same code again and again is a bad thing. But in every rails application, we can often see similar CRUD actions in every controller which violates the DRY principle of Rails. To avoid such kind of duplicacy, we can club the standard CRUD actions at one place. Resources Controller is a plugin to facilitate this approach and to keep your controllers DRY. It introduces some abstraction to help you override the default RESTful functionality in a clean and simple manner. How to use:-
  • To use Resources Controller, firstly you need to download it from https://github.com/ianwhite/resources_controller and keep it in vendor/plugin folder of your Rails application.
  • In every controller of your application which needs to get this nifty functionality, you just add resource_controller_for line in the controller.
     class PostsController < ApplicationController
           resources_controller_for :posts
       end
    
    now your posts controller will get all standard CRUD actions, views will get instance variable (i.e. @post or @posts).
  • Here are some general methods of Resources Controller which can be used in your controller for convenience and for making your code RESTful.
  • enclosing_resources: returns array of resources which are nested in that controller.
  • resource_class : returns class of controller’s resource.
  • find_resources: returns collection or array of resources (commonly used in index).
  • find_resource : returns the resource using id from params (commonly used in show, edit, update & destroy).
  • new_resource : initializes a resource of controller’s resource class (commonly used in new).
  • save_resource : saves the newly created or updated resource (commonly used in create).
  • resource : returns the controller’s current resource.
If you want to add some extra functionality into your actions, you can write that action in your controller. Now your newly written action will override the standard action defined in Resources Controller. Isn’t it the Rails DRYness at its best !!

Installing mysql gem on MAC OS X

Recently, we had to set up an RoR application on a client machine. Everything went on smooth till we got to the point where bundler threw an error complaining about being unable to build the mysql gem. You will most probably be headed to the same problem if you download and install mysql community server from mysql downloads page. Here is the error that you will most probably be getting while installing the msyql gem:

Building native extensions. This could take a while…
 ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install mysql — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include
 checking for mysql_query() in -lmysqlclient… no
 checking for main() in -lm… yes
 checking for mysql_query() in -lmysqlclient… no
 checking for main() in -lz… yes
 checking for mysql_query() in -lmysqlclient… no
 checking for main() in -lsocket… no
 checking for mysql_query() in -lmysqlclient… no
 checking for main() in -lnsl… no
 checking for mysql_query() in -lmysqlclient… no
<ul>
	<li>extconf.rb failed <strong>*</strong>
 Could not create Makefile due to some reason, probably lack of
 necessary libraries and/or headers. Check the mkmf.log file for more
 details. You may need configuration options.</li>
</ul>
Provided configuration options:
 —with-opt-dir
 —without-opt-dir
 —with-opt-include
 —without-opt-include=${opt-dir}/include
 —with-opt-lib
 —without-opt-lib=${opt-dir}/lib
 —with-make-prog
 —without-make-prog
 —srcdir=.
 —curdir
 —ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
 —with-mysql-config
 —without-mysql-config
 —with-mysql-dir
 —with-mysql-include=${mysql-dir}/include
 —with-mysql-lib=${mysql-dir}/lib
 —with-mysqlclientlib
 —without-mysqlclientlib
 —with-mlib
 —without-mlib
 —with-mysqlclientlib
 —without-mysqlclientlib
 —with-zlib
 —without-zlib
 —with-mysqlclientlib
 —without-mysqlclientlib
 —with-socketlib
 —without-socketlib
 —with-mysqlclientlib
 —without-mysqlclientlib
 —with-nsllib
 —without-nsllib
 —with-mysqlclientlib
 —without-mysqlclientlib

Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql-2.7 for inspection.
 Results logged to /Library/Ruby/Gems/1.8/gems/mysql-2.7/gem_make.out

Now what this error is telling is that the C compiler is unable to find the libraries required to build the gem. Although all the libraries are present but this error may still occur if the path is not set to the mysql local installation which defaults to /usr/local/mysql if you install the dmg archive for MAC on the mysql downloads page. To fix this, add the following line to your .bash_profile on MAC machine: [C] PATH=${PATH}:/usr/local/mysql/bin[/C] This should immediately solve the problem of the missing libraries and the gem should build fine. Enjoy mysql with Rails on MAC OS X.