Create your first Rails Application
I write this article to give an idea on creating a rails application from the beginning. This article will be more suitable for beginners with some programming knowledge.
What is Rails ?
Rails is a web application framework written in Ruby Language. Programming web application will become easier from this.
I am developing rails application since last year and according to my experience I write less code to accomplish some task when it compares to other web programming languages. (I have tried J2EE and PHP). If you want to study Rails, there is nice guide for you. That;s the official guide. Check it using the following link.
And to develop rails applications you should have some knowledge about Ruby as well. Ruby is the heart 💓 of Rails.
Use the following link to learn ruby.
Ruby Tutorial - Try Ruby
Learn the basic building blocks of the Ruby programming language in Code School's Try Ruby course.
How to setup the computer for Rails
First we need to setup the computer in order to program ruby on rails. I am using Ubuntu 16.04 LTS for this. I am using the terminal coming with RubyMine 2017 and Atom as the editor.
RubyMine: Ruby on Rails IDE by JetBrains
A hackable text editor for the 21st Century
At GitHub, we're building the text editor we've always wanted: hackable to the core, but approachable on the first day…
- Install Ruby
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs
2. You will need rbenv when you want switch between ruby versions. So install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
rbenv install 2.3.0
rbenv global 2.3.0
you can check the installed ruby version
3. Install Bundler
Bundler is the best way to handle and manage ruby gems.
Bundler: The best way to manage a Ruby application's gems
Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that…
gem install bundler
4. Add Node JS Dependencies
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
5. Install Rails
Rails is actually a gem. So we install it like a gem
gem install rails -v 4.2.3
Latest version is 5.0.0 now.
6. Run the following command to make the rails executable available
Let’s create the first application
- Run the create command to create rails application
version name of the application
rails _4.2.3_ new myapp
here you can choose the database type you need as well. (MySQL, SQLlite, PostgreSQL).
rails _version_ new AppName -d DB_TYPErails _4.2.3_ new MyFirstApp -d mysql
If you didn’t put -d in the code rails will create the application with SQLlite default.
When you run this command in the URL
2. Go to the created Folder
3. Run Bundle install to complete installing gem files
you should get the successful message like this.
Bundle complete! 12 Gemfile dependencies, 61 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
4. Now I will open my folder via Atom for the development.
Above command will work only if you have install the atom correctly
5. Create the database according to the config database file.
password: <%= ENV['MYFIRSTAPP_DATABASE_PASSWORD'] %>
Above is the database.yml file under the config directory. If you have a MySQL password you enter it here and at the same time create a database with this name in your MySQL console.
6. Run the Rails Server
rails serverorrails s
7. Open the Browser and Enter the URL
Initially it should look like this
Done ! , Now we have successfully created our first application using Ruby on Rails.
After this you can create controllers, view needed to develop the application useful.
This Rails application has MVC structure. If you want to read about MVC in rails I recommend the following article.