CMS Functionality

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