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_atattr_accessiblein your Post model and then add the following to your admin view:

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).

classAdmin::PostsController<Admin::AdminController#only showing the update methoddefupdate@post=Post.find(params[:id])if@post.valid?add_author_to_postsave_or_remove_post_categorizationendrespond_todo|format|if@post.update_attributes(params[:post])format.html{redirect_to[:edit_admin_post],:notice=>'Post was successfully updated.'}elseformat.html{render:action=>"edit"}endendend#method created for adding user to posts tabledefadd_author_to_post@user=User.find_by_id(session[:user_id])@user.posts<<@postendend

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.

About

Hi, I'm Eileen! I'm a Senior Systems Engineer at GitHub. I'm an avid contributor to Open Source and am on the Rails Core Team and the Rails Security Team. I enjoy speaking at conferences, usually about performance or contributing to Open Source. Other than writing code, I enjoy craft beer and hiking with my husband, Abe and our dog, Arya.