What is Rails?
Rails is a web application framework written in Ruby Language.Programming web application will become easier by this. I am developing railsapplications since last year and according to my experience I write less codeto accomplish some tasks when compared to other web programming languages. (Ihave tried J2EE and PHP). If you want to study Rails, there is a nice guide foryou. 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 this link tolearn ruby.
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.
1. Install Ruby
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-devlibreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-devlibcurl4-openssl-dev python-software-properties libffi-dev nodejs
2. You will need rbenv when you want switch between ruby versions. Soinstall rbenv
gitclone https://github.com/rbenv/rbenv.git~/.rbenv
echo'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo'eval "$(rbenv init -)"' >> ~/.bashrc
exec$SHELL
gitclone https://github.com/rbenv/ruby-build.git~/.rbenv/plugins/ruby-build
echo'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >>~/.bashrc
exec $SHELL
rbenvinstall 2.3.0
rbenvglobal 2.3.0
you can check the installed ruby version
ruby -v
My PC Ruby Version
3. Install Bundler
Bundler is the best way to handle andmanage ruby gems.
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. Latest version is5.0.0 now.
gem install rails -v 4.2.3
6. Run the following command to make the rails executable available
rbenv rehash
Let’s create the first application
Run the create command to create rails application
version name of theapplication 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_TYPE rails _4.2.3_ new MyFirstApp -dmysql
If you didn’t put -d in the code rails will create the application withSQLlite default. When you run this command in the URL
2. Go to the created Folder
cd MyFirstApp
3. Run Bundle install to complete installing gem files
bundle install
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.
atom
Above command will work only if you have install the atom correctly
5. Create the database according to the config database file.
default: &default adapter:mysql2 encoding: utf8 pool: 5 username: root password: socket: /var/run/mysqld/mysqld.sockdevelopment: <<: *default database: MyFirstApp_development test: <<: *default database: MyFirstApp_test production: <<: *default database: MyFirstApp_production username: MyFirstApp password: <%=ENV['MYFIRSTAPP_DATABASE_PASSWORD'] %>
Above is the database.yml file under the config directory. If you have aMySQL password you enter it here and at the same time create a database with thisname in your MySQL console.
6. Run the Rails Server
rails server or rails s
7. Open the Browser and Enter the URL
localhost:3000
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 theapplication useful. This Rails application has MVC structure. If you want toread about MVC in rails I recommend this article.