Post model generated Code

class Post < ActiveRecord::Base
  belongs_to :blog
  attr_accessible :text, :title
end

Post model adapted Code

class Post < ActiveRecord::Base
  attr_accessible :body, :title
  belongs_to :blog
  has_many :comments, :dependent => :destroy
  validates :title, :presence => true, :length => { :maximum =>150 }
  before_save do |post|
    post.permalink = post.title.parameterize
  end
  def self.search(search)
    if search
      search = '%'+search+'%'
      where('(title LIKE ?) OR (body LIKE ?)', search, search)
    else
      find(:all)
    end
  end
  def to_param
    self.permalink
  end
end


Post controller generated code

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, :notice => 'Post was successfully created.' }
        format.json { render :json => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.json { render :json => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, :notice => 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

Post controller Adapted Code

class PostsController < ApplicationController
  before_filter :signed_in_user, :o nly => [:new, :create, :edit, :update, :destroy, :myindex]
  before_filter :correct_user,   :o nly => [:edit, :update, :destroy]
  def show
    @post = Post.find_by_permalink(params[:post_permalink])
  end
  def new
    @post = Post.new()
  end
  def index
    @page_count = Post.search(params[:search]).count/3
    @page_count += 1 unless Post.search(params[:search]).count%3 == 0
    @posts = Post.order('created_at DESC').limit(3).offset(params[:offset]).search(params[:search])
    if signed_in?
      @user = User.find(current_user.id)
      render 'posts/signed_user_frontpage'
    else
      render 'posts/index'
    end
  end 
  def user_name_index
    @user = User.find_by_name(params[:user_name])
    @blog = @user.blog
    if @user
      @page_count = @blog.posts.count/3
      @posts = @blog.posts
      render 'index'
    else
      render :inline =>
        "<h1> Error: that user does not exists </h1>"
    end
  end
  def create
    @post = Post.new(params[:post])
    @post.blog_id = current_user.blog.id
    if @post.save
      flash[:success] = "Your post has been published!"
      redirect_to post_path(@post.blog.user.name,@post.permalink)
    else
      render "new"
    end
  end
  def edit
    @post = Post.find_by_permalink(params[:post_permalink])
  end
  def update
    @post = Post.find_by_permalink(params[:post_permalink])
    if @post.update_attributes(params[:post])
      flash[:success] = "Post updated"
      redirect_to post_path(@post.blog.user.name,@post.permalink)
    else
      render 'edit'
    end
  end
  def destroy
    @post = Post.find_by_permalink(params[:post_permalink])
    @post.destroy
    redirect_to :back
  end
  private
    def correct_user
      post = Post.find_by_permalink(params[:post_permalink])
      user = User.find_by_name(params[:user_name])
      unless current_user?(user)
        flash[:notice] = "You are not allowed to do that"
        redirect_to(root_path)
      end
    end
end