How to create a Ruby on Rails application .
By: Lukasz Muzyka, On:
This tutorial assumes you have already completed:
Ruby on Rails is a great framework widely used by many small, medium, as well as large organizations. In the web world, ruby provides a great tool for prototyping and getting your ideas executed really, really fast..
Ruby on Rails is not a language. It is a set of tools developed in the Ruby language and it allows you not to worry about all the configurations that you need to go through when starting your project.
When I was starting to learn to code, not having to setup my project architecture was an awesome thing. If I had to go through all the setup and configurations, I wouldn't have learned how to actually write code.
Ruby on Rails competes with various frameworks using other languages such as Python, Java, Php and most recently node.js
It has many advantages such as a robust and generous community, great documentation, great security features and most of all, it's easy to learn and use for very fast development. To give you an idea, this website has been built from scratch in about 20 hours using Ruby on Rails. At the time of writing, this website allows users to create an account, access premium content, comment on it, add custom avatars and submit questions via a contact page. Administrators can manage content generated on the platform, browse analytics, receive reports on new users signed up, and questions asked via the contact page.
Step 1: Create the App
Creating the app is very easy. In case you didn't install Ruby on Rails on your machine yet, go to this page and follow the instructions.
Then, come back here, open your terminal and write:
$ cd Documents # open directory "Documents" $ mkdir projects # create new folder "projects" inside "Documents" $ cd projects # open directory "projects" $ rails new demo # generate new application
After you run this command it should give you something like this:
Running this command may take a while. Your computer is now downloading all the necessary components to build your application and will notify you when it's complete:
Fetching gem metadata from https://rubygems.org/.......... Fetching additional metadata from https://rubygems.org/.. Resolving dependencies... Installing rake 10.3.1 Using i18n 0.6.9 Using json 1.8.1 Using minitest 5.3.3 Using thread_safe 0.3.3 Using tzinfo 1.1.0 Using activesupport 4.1.0 Using builder 3.2.2 Using erubis 2.7.0 Using actionview 4.1.0 Using rack 1.5.2 Using rack-test 0.6.2 Using actionpack 4.1.0 Using mime-types 1.25.1 Using polyglot 0.3.4 Using treetop 1.4.15 Using mail 2.5.4 Using actionmailer 4.1.0 Using activemodel 4.1.0 Using arel 22.214.171.12440414130214 Using activerecord 4.1.0 Using bundler 1.6.2 Installing coffee-script-source 1.7.0 Installing execjs 2.0.2 Installing coffee-script 2.2.0 Using thor 0.19.1 Using railties 4.1.0 Installing coffee-rails 4.0.1 Using hike 1.2.3 Installing multi_json 1.9.3 Installing jbuilder 2.0.6 Installing jquery-rails 3.1.0 Using tilt 1.4.1 Installing sprockets 2.11.0 Using sprockets-rails 2.1.3 Using rails 4.1.0 Installing rdoc 4.1.1 Installing sass 3.2.19 Installing sass-rails 4.0.3 Installing sdoc 0.4.0 Installing spring 1.1.2 Installing sqlite3 1.3.9 Installing turbolinks 2.2.2 Installing uglifier 2.5.0 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. Post-install message from rdoc: Depending on your version of ruby, you may need to install ruby rdoc/ri data: <= 1.8.6 : unsupported = 1.8.7 : gem install rdoc-data; rdoc-data --install = 1.9.1 : gem install rdoc-data; rdoc-data --install >= 1.9.2 : nothing to do! Yay! run bundle exec spring binstub --all * bin/rake: spring inserted * bin/rails: spring inserted
Step 2: Open the source
You're almost done. Your application is now ready to go. We can now open it and see the source code:
$ cd demo $ ls
The "ls" command will list all the contents of the directory. At this point you can also open it in your finder. Simply, go to
Documents/projects/demo . If you want to understand better about commands like "ls" or "cd", check this cheatsheet.
Step 3: Run the Application
In the terminal, type:
$ rails server
=> Booting WEBrick => Rails 4.1.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option) => Ctrl-C to shutdown server [2014-04-28 10:46:11] INFO WEBrick 1.3.1 [2014-04-28 10:46:11] INFO ruby 2.1.1 (2014-02-24) [x86_64-darwin13.0] [2014-04-28 10:46:11] INFO WEBrick::HTTPServer#start: pid=60252 port=3000
This will start rails server to "listen" in on port 3000. To see what that means, open your internet browser and type the address: http://localhost:3000
This is how your application looks. We didn't write any code yet, but Rails comes with this interface built in to help us make sure everything works as intended. We will change it very soon to perform all the tasks we may want to have in mind.
You can only run one server on port 3000. If you already have one application running when you may want to start another otherwise it may give you an error. So make sure you stop the server after you're done by pressing "control + C" on Mac.
Also, it's a good idea to start using a dedicated editor at this point to write code. Technically, you can write code in almost any text editor as simple as Windows Notepad but a plethora of free and great code editors make it a lot easier for you. So go ahead and download Sublime Text if you haven't so far. Once you're done, open "demo" folder in the editor to browse all your source code.
You can login to comment