Eileen speaking at Rails World The Myth of the Modular Monolith at Rails World. Photo by Aaron Patterson.

Functionality Recently Added to eileenbuildswithrails.com

I've been working on this blog in bits and pieces, when I have time. I've added some new functionality to bring it closer to a real blog CMS.

Adding Published at date changer

First I added a Published at date changer so that I could change my post dates if necessary. I think I'm going to do a "definitive list of RoR helper methods" because if it weren't for my pledge to watch all the railscasts videos in under a year I wouldn't have know about date_select and datetime_select and I would have been putting my head through a wall trying to figure it out. I feels almost like Rails cares about my sanity!

To use this helper method make :created_at attr_accessiblein your Post model and then add the following to your admin view:

<div class="field">
   <%= f.label :created_at, "Published at" %>
   <div class="datetime">
      <%= f.datetime_select :created_at %>
   </div>
</div>

Belongs_to user for posts

I figured in the future I may have some guest bloggers, and it would be nice now to have all the posts have an owner so I woundn't need to go back and add it later.

First I created a migration so that would add the current users ID to the post table. Then I updated the post and user model adding belongs_to :user to the post model and has_many :posts to the user model.

Next, I wrote a method so I could call it in multiple actions if I needed to (for at least the first three posts I wanted to call the method on update to add the user to the table, also I only have one user so I knew nothing would happen).

class Admin::PostsController < Admin::AdminController

  #only showing the update method
  def update
    @post = Post.find(params[:id])
    
    if @post.valid?
      add_author_to_post
      save_or_remove_post_categorization
    end
     
    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to [ :edit_admin_post ], :notice => 'Post was successfully updated.' }
      else
        format.html { render :action => "edit" }
      end
    end
  end
  
  #method created for adding user to posts table
  def add_author_to_post
     @user = User.find_by_id(session[:user_id])
     @user.posts << @post
  end
end

In my view I was getting my most hated errror "You have a nil object when you didn't expect it". Yea Rails, if I expected it we wouldn't be haivng this conversation...

Anyway, I quickly figured it out was because I did not already have my author filled in so of course rails was complaining. Here is my view for the author section.

<div class="datetime">
   <% if @post.user.nil? %>
      No author specified
   <% else %>
      <%= @post.user.username %>
   <% end %>
</div>

The Future

The next stesp will be adding the a lot more functionality and I will post on them as I do. I want to add:

  • The ability for comments
  • Ability to create draft, and future posts
  • Contact Form
  • Custom sidebar twitter feed
  • Post tweets from admin for new post notification
  • Login and profiles for guest bloggers

Functionality way further down that line but will be really awesome

  • Front-end customization from admin area
  • Creation and reorder for dynamic menus (think WordPress drag and drop menus)
  • Ability to add and customize modules
  • And much more...

Part 1: Launching Eileen Builds with Rails: Deployment with Capistrano, Unicorn, Nginx on Centos6

Note: I never did finish parts 2-4 for this blog post. I waited too long to write them and forgot how they worked. I've left this post up even though it's not really valid any longer. There are much better articles now on getting this running.

I'm going to do this post in four parts. Part 1: checklist things to do before deployment, Part 2 Capistrano, Part 3: Unicorn, and Part 4: Nginx. I also don't have the ability for draft posts set up yet, so...instead I'm publishing in parts so I don't need to write it all at once.

Launching this Rails app was not easy, although since I had never deployed an app, I wasn't expecting it to be easy. There are a bunch of little things you need to know about deployment if it's your first time that I think it would be helpful for other first-timers. Also, when you've been working on that web app for 24 hours straight you forget the all important mental checklist.

Please feel free to correct me to tell me I'm "doing it wrong" because this is my first deployment and I will fully admit I might have no idea what I'm doing.

Preparing Your App for Deployment

One thing I failed to do and I regret a lot is making sure certain files aren't include in git with my .gitignore. Files to remove are:

  • db/*sqlite3
  • log/*.log
  • public/assets* (now I did this because I wasn't using any public assets, and didn't want the rouge stylesheets being added)
  • Gemfile.lock

Chances are if your app is running on sqlite3 in development it won't be in production and will more likely be using a MySQL database or PostgreSQL. My app is using MySQL so we'll go over setting up your Gemfile for that.

If you have just gem 'sqlite3' in your Gemfile you'll need to change it to what I have below. While here you'll probably want to add the gems needed for deployment as well.

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'mysql'
end

# Use unicorn as the web server
gem 'unicorn'

# Deploy with Capistrano
gem 'capistrano'
gem 'capistrano-unicorn'

Before deploying you'll need to precomile your assets with:

rake assets:precompile

This is really important because I forgot this step and could not figure out why my assets weren't showing up and I was getting a lot of "No Route Matches" in my logs. I didn't find this anywhere other were having this problem and no one said "hey maybe you forgot this really important thing."

Once capistrano is set up you can automate this task during deployment, but we'll cover that in Part 2.

When Parts 2-4 are written they will be linked to at the bottom of this post.

New Year, First Post

An Introduction

First, Happy New Year to everyone. This blog isn't a New Year's Resolution, so don't worry I won't write in it for 15 days and then never appear online again. 

I started this blog for a couple reasons. I went to Big Nerd Ranch in September to learn the basics of Ruby and Ruby on Rails, and in that class we learned one of the ways we could contribute to the Rails community was writing a blog. I wanted a way to document and collect the things I learned, the mistakes I made, and also hope to help other beginners. 

It will also help me hold myself accountable - to always be learning new things about Rails and giving back to the open source community.

From Photographer to Developer

When I thinking about how I got where I am now I can't help but laugh a little. I can tell you that if you asked me where I'd be now 5 years ago, I wouldn't have said where I am now. In high school, I focused a lot on art classes and thought perhaps I wanted to be an art educator and went to college believing that's what my major would be.

I think I was about 5 seconds into my freshman year when I realized teaching art was not something I wanted to do. I decided on a photography major and although I wish I had realized my passion for web development and computer science much earlier on I am grateful for all the creative energy I was required to use during my time as a photography major. I think I learned some valuable things that can easily be applied to where I am today, for example, the importance of maintaining the scope of a project. 

In my senior year of college I took a Flash class for two reasons; it fit well in my schedule and I wanted to learn how to build a basic portfolio site for myself. As our final project I built my original website. At first my professor discouraged me from doing this because with only one month to complete the project he felt the timeline was a little to ambitious. I strive to constantly challenge myself. and I did in fact finish it ontime - with lots of hours over Thanksgiving break. 

That was 3? 4? years ago I lose track. I took another class or two, but most of what I learned was on the job or things I taught myself. I have learned so much in a few years I cannot believe the information I absorbed. 

For the last two and a half years I have been working for Evolving Media Network, a web development company in New York. Starting out with static html websites I quickly taught myself enough PHP to hack together WordPress sites and that was the majority of my responsibilities for a few years.

The Present

This past September I went to the Big Nerd Ranch in Atlanta to attend their Beginning Ruby and Ruby on Rails class. It was awesome and if you're thinking of going it's a great learning environment and the instructors are talented and helpful. Since then I have been practicing Rails by working on my own projects and have a few ideas for gems I hope to get to soon. 

You can also follow me on twitter @eileencodes

Thanks for reading!

Categories: learning-rails

Hello world

I just launched this blog and hope to have some informative posts about what I'm learning up soon. 

A shout out to the only reasons this blog is running: 

Ruby
Ruby on Rails

Capistrano
Nginx
Unicorn

Code I post on this site will be gists from Github, and I couldn't have built this without RailsCasts.

Categories: shoutouts