Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adventures of a Rails Apprentice – Part 2

DZone's Guide to

Adventures of a Rails Apprentice – Part 2

· Web Dev Zone
Free Resource

Should you build your own web experimentation solution? Download this whitepaper by Optimizely to find out.

I wrote about some of the pitfalls of getting started with Rails in part 1. In this, and subsequent parts, I’ll cover what it was like building my first Rails app.

The app I decided to build was something like an article manager. Not a blog, but somewhere I could store draft articles (written using Markdown) and be able to add/edit drafts along the way.

App Requirements

So specifically I wanted:

  • An app that looked reasonable without too much effort spent on design.
  • A public interface for viewing the articles and ideas.
  • An admin area for working on the articles.

Getting Started with Rails

I decided that if this app was ever going to see production, it would most likely run on Heroku. So I decided to use Postgresql for the database since that’s what Heroku supports.

I also decided to use Twitter Bootstrap to look after the design elements. So I added gem 'bootstrap-sass' (see info here) to my Gemfile, and followed it with the usual bundle install via terminal.

The next decision was what to do about the admin area. If I built it myself, I would need to be dealing with authentication and login, whereas if I used a gem specially for creating an admin area, there would be a learning curve there too.

I decided to go with the ActiveAdmin gem because I figured it was important to get something up and running quite quickly for my first app. Plenty of time for further experimentation further down the line.

I Struggle With Data First

I know that with Rails, everything ends to start with the model. I struggle with that, so I decided to go with getting at least something to look at working first.

That meant coming up with a layout, and adding some custom styles in the asset pipeline.

Layout and Styles

I already had the Bootstrap support installed. So I borrowed the narrow layout example from the Twitter Bootstrap examples to add a layout to application.html.erb in views/layouts.

I also created custom.css.scss in app/assets/stylesheets an imported all the Bootstrap stuff using this:

@import "bootstrap";

The example from Bootstrap has some custom styles for the layout, so I added those to custom.css.scss too. You’ll find them in the <head></head> section of the example file.

All I had to do in the layout, was in <%=yield %> and the usual Rails calls to the stylesheets and any Javascript stuff in the document head:

Making a Controller

You need a controller to get a basic layout working properly, so from Terminal I did:

rails g controller index about contact blog

At this stage, I have no idea whether those actions will be the ones I need, but it seems like a start.

Adding a Route

I removed the index.html file from the public folder, and then in app/config/routes.rb I added:

root :to => 'drafts#index'

Trying it Out

Don’t laugh, but I fired up the Rails server and went to http://localhost:3000 in my browser. It didn’t work of course, because I created the project from Terminal like this:

rails new drafts -d postgresql

So I needed to add my database credentials in app/config/database.yml and then run rake db:create.

After re-starting the server I now got see my early attempts:

A basic rails app

A basic app

Finally…

So I have a very basic skeleton up and running, with the database configured. In part 3, I’ll continue to flesh out the features I want for the app.

Implementing an Experimentation Solution: Choosing whether to build or buy?

Topics:

Published at DZone with permission of Andy Hawthorne, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}