This is the first working post in the Ruby on Rails Tutorials series. The primary goal of this series is to help others learn Ruby on Rails through my attempts at learning this web development framework. I’ve chosen Ruby on Rails as the language for this series due to my propensity for following the Lean Startup methodologies proposed in the books: The Four Steps to the Epiphany by Steve Blank,The Lean Startup by Eric Ries and Running Lean by Ash Maury
I’ll also be using the RubyMine development environment for the bulk of this tutorial. RubyMine is a purpose built application to support developing, testing and deploying Ruby on Rails applications. While, you can perform all of these actions in a native text editor and command line, a dedicated development tool, has prebuilt commands and links to help make your development faster.
Section 1: Setting up the Ruby on Rails web application
Section 2: Building the GIT Repository
There, now that we have a working rails home page. Lets setup our local Git Repository, so that we can keep track of our changes, so if we do have an issue we can roll back to the step prior, without losing much work. Inside of RubyMine, this will be accomplished by again, going to the terminal. At the prompt, type git init and press enter. You’ll be presented with “Initialized empty Git repository in /Users/Bill/RubymineProjects/taskManager/.git/“
Now, lets see what we currently have in our git repository. If you run git status, you’ll see list of files that have not been added to a git repository. So, the next thing that we need to do is to add the missing files. We accomplish this my typing “git add .”, And yes the “.” Is part of the command. Now, if you run the git status, you’ll see that the files are ready to be checked in
So, currently, git knows about our files, but they aren’t committed yet. You can see this by running “git branch”. Here, you’ll see that there are no branches. The files are ready to be committed, but no branch exists. To create our first branch, we’ll type pay attention to the double quotes around initial.
git commit -am “initial”
This is the title of your commit, going forward, if your comment should give a quick overview of what you did in this commit. Examples: Added task status model, configured task status to has many tasks, etc. After pressing enter, you’ll see a list of create mode ######, this is the git app loading your files into the git repository. Now if you run git branch, it will return * master. Your now running a local git protected rails app. I’ll write up the instructions for using to BitBucket in a later post.Finally, the next few actions are to tell git, who you are. This is accomplished by entering the following, changing the name to your name.
git config –global user.name “Your Name”
git config –global user.email firstname.lastname@example.org
git commit –amend –reset-author
Section 3: Installing the configuration gems
Installing the Zurb Foundations in Ruby on Rails.
A default Ruby on Rails application is very plain and not very appealing. To overcome this, we’ll be configuring Zurb Foundations through the use of the foundation specific gems. Quick aside, about gems. A great resource for finding a gem, which there are thousands, is rubygems.org. Normally, you can find a gem that already covers much of what you want to do in your Ruby on Rails web app. Authentication, authorization, testing, UI customization, administration, etc…. From ruby gems, you can go to the gems homepage and see the steps for configuring the gem in your web application. For now, lets focus on the Foundations gems: foundation-rails and foundation-icons-sass-rails. We need to go into the gem file, that is found in the root of your web app folder. You can place the following code anywhere in this file. I prefer to place near the top. The hash tag indicates that this line is a comment, and will be ignored by Ruby.
# advanced responsive front-end framework
After saving the changes, RubyMine will prompt you that there are uninstalled gems added to your gemfile. Click on the link and follow the instructions to install the gems. If you are working from the command line, or just prefer, you can also type “bundle install” from the terminal and perform the same steps as RubyMine accomplished. Finally, we need to install foundations on our app. Many gems, will have additional files or edits to existing files that need to be accomplished prior to using the gem. Foundations is one such gem. When your run the command rails g foundation:install, you’ll receive the following output.
Most of this is information only, it is rails telling you that it is modifying your web app to support the gem. Their is one action from you though. The line that has the red “conflict”, this line says that the file already exists and will be overwritten. You have to either accept the overwrite by pressing enter or telling it no. For us, there is no code yet, so modifying the application.html.erb file is not a big deal. But, if you have already modified this file, than you would not want to have it overwritten. So for now, after letting the install overwrite our application.html.erb file. We have a working Ruby on Rails web app with a customizable responsive front-end framework.
Installing the simple form gem
Next, we’ll add a gem that will allow us to create and modify our view pages with less code. You can see that in a simple scaffold example that we’ll build in the next post.
Here is an example of the default scaffold:
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<%= pluralize(@task.errors.count, “error”) %> prohibited this task from being saved:
<% @task.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :status %>
<%= f.text_field :status %>
<%= f.label :order %>
<%= f.number_field :order %>
<%= f.label :duedate %>
<%= f.datetime_select :duedate %>
<%= f.label :location %>
<%= f.text_field :location %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.submit %>
<% end %>
And here is the same form, generated in simple_form.
<%= simple_form_for(@task) do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.input :status %>
<%= f.input :order %>
<%= f.input :duedate %>
<%= f.input :location %>
<%= f.input :description %>
<%= f.button :submit %>
<% end %>
To setup simple_form, perform the following actions.
First, Update the gemfile to include the following:
# Framework to make building new forms
gem ‘simple_form’, ‘~> 3.1.0’
After saving the gem file, run the following
rails generate simple_form:install –foundation
You now should be able to start rails and verify that it is visible. It should still be running just go and verify that the page is still up.
Blog Post Wrapup
So, what have we accomplished so far in this Ruby on Rails tutorial? First we generated our rails application using the RubyMine development tool. We’ve created our local software repository using Git. And we’ve configured our Ruby on Rails application to use Zurb Foundations and Simple_form. We’ve verified that our app is listening and we can view the default Ruby on Rails
So whats next? Well, the exciting part of building a Rails application. You can think of this like building a cake. We just mixed up the cake batter and threw it into the oven. The next few posts will be about decorating the cake and making it more specific to the celebration at hand.