Lets Make a Rails Application part 1
First before we get to developing our application we require a few more utilities so we can successfully create on windows. First we need to download Ruby Development Kit, http://dl.bintray.com/oneclick/rubyinstaller/DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe extract this to a directory open a command window in this directory and run these three commands
ruby dk.rb init
ruby dk.rb install
gem install rdiscount --platform=ruby
Congratulations Ruby Development Kit has been installed, next we require Node.js https://nodejs.org/dist/v4.4.0/node-v4.4.0-x64.msi follow that installer and Node.js should be fully installed.
Lets make that project
Now before we make a project we should figure out what type of project we want to make, it has been decided we will make a basic blog with Create, Read, Update, and Destroy (CRUD) functions. since this is a completely local project we will call it simply blog. The creations command to create a new project is as follows
rails new blog
Keep in mind this command will create the new directory exactly where the command window current directory is. We need to now start working in our new project directory
Starting a web server with Rails is fairly easy, the automatic port rails works with is 3000 so once the server is hosted, to view our rails webpage we enter localhost:3000 into out web browser. the command to start the rails web server is as follows
If we access our newly created web server we are greeted to a welcome aboard page hosted by Ruby on Rails, this page has links to Rails guides, the Rails API and Ruby standard library.
The first HTML page
First thing any webpage needs is a welcome page, Rails requires both a controller and a view to make it’s first HTML page. A controller is what handles actions within Ruby, view are what displays the information held by the controller into easily readable data. To put it in simple terms the controller is the brain behind the operation and the view is the pretty model that everyone wants to look at. using the following command Rails will generate both a controller and a view. Keep in mind if you have the server running the command window will be too busy to do any other operations, we should keep the server running and open a new command window in the blog directory.
Rails generate controller welcome index
This creates a number of files within the blog directory, the one we are currently interested in is located at blog/app/views/welcome under the file name index.html.erb the code auto generated in this will be as follows
<p>Find me in app/views/welcome/index.html.erb</p>
Now using the HTML knowledge you have at your disposal you may change this page as much as you like, the focus of this blog is not HTML, therefore we are gonna to change this to a really simple page for now, the following is what i replaced for this page
<h1> Welcome to my Blog, it's still under construction</h1>
Now currently this page is not our root for our web application, to change the home webpage we have to modify the blog/config/route.rb file. the following is the auto generated file that we will be changing.
If we remove the # in front of “ root ‘welcome#index’”, we will remove the commenting out of that line. this will force the index action of the welcome controller to be the home webpage of our web service. if we were to open our browser to localhost:3000 once again we would see the HTML page we just created. While we have the route.rb file open we should add the last thing we need to add to this file for the finalized project. We want to make a new database table known as articles, to accomplish this we will add resources :articles to this file E.G.
This will make sure our entire project can access the articles table. Rails does need a controller for each table to handle any actions that happens with this table, so lets generate that with the following command.
rails generate controller articles
We should note since we have no arguments after articles the controller will have no actions upon generations so we will have to make our own. Which we will on the next blog post.