Hello,
I’m trying to make this controller RESTful, but its post method uses
two other methods – select_category and show_form – see them in
action at http://demo.chuckslist.org/ads/post. How do I reduce all of
this to just one method? Other advice on making this controller as
RESTful and beautiful as possible would ofcourse be much obliged.
Here’s the controller:
class AdsController < ApplicationController
def show
@ad = Ad.find_by_id(params[:id])
if (@ad.nil?)
flash[:warning] = “ad doesn’t exist”
redirect_to root_path
end
end
def destroy
if request.post?
@ad = Ad.find_by_activation_code(params[:id])
if (@ad.nil?)
flash[:warning] = “ad doesn’t exist”
redirect_to root_path
else
@ad.destroy
flash[:notice] = “ad removed”
redirect_to root_path
end
end
end
def list
@category = Category.find_by_slug(params[:slug])
if @category
@ads = @category.ads.all_active
@requested_category = @category.name + ’ in ’ +
@category.parent_category.name
else
@category = ParentCategory.find_by_slug(params[:slug])
if @category
@ads = @category.all_ads
@requested_category = @category.name
else
flash[:warning] = “invalid request”
redirect_to root_path
end
end
end
def post
@parents = ParentCategory.find :all, :order => ‘name ASC’
end
def select_category
@parent_category = ParentCategory.find_by_id(params[:id])
end
def show_form
@category = Category.find_by_id(params[:id])
end
def new
if (params[:email] != params[:email_confirmation])
flash[:warning] = “e-mails don’t match”
redirect_to :controller => ‘ads’, :action => ‘post’
else
@author = Author.find_by_email(params[:email])
if @author.blank?
@author = Author.new
@author.email = params[:email]
@author.ip = request.env[‘REMOTE_ADDR’]
@author.save
end
@ad = Category.find_by_id(params[:category]).ads.new
@ad.title = params[:title]
@ad.ad = params[:ad].gsub(“\n”, “
”)
@ad.expiration = Time.now + 30.days
@ad.author = @author
@ad.author_ip = request.env[‘REMOTE_ADDR’]
@ad.save
@ad.handle_images(params[“image_attachments”])
Mail.deliver_activation(@ad, @author.email)
flash[:notice] = “ad pending activation”
end
end
def activate_ad
@ad = Ad.find_by_activation_code(params[:activation_code])
if (@ad.nil?)
flash[:warning] = “error activating ad”
redirect_to root_path
else
if @ad.activate_ad(params[:activation_code])
flash[:notice] = “ad activated”
Mail.deliver_activated(@ad, @ad.author.email)
redirect_to :action => ‘edit’, :activation_code =>
@ad.activation_code
else
flash[:warning] = “error activating ad”
redirect_to root_path
end
end
end
def manage
@ad = Ad.find_by_activation_code(params[:activation_code])
if (@ad.nil?)
flash[:warning] = “ad doesn’t exist”
redirect_to root_path
else
end
end
def edit
@ad = Ad.find_by_activation_code(params[:activation_code])
if (@ad.nil?)
flash[:warning] = “ad doesn’t exist”
redirect_to root_path
else
end
end
def update
@ad = Ad.find_by_activation_code(params[:activation_code])
if (@ad.nil?)
flash[:warning] = “ad doesn’t exist”
redirect_to root_path
else
@ad.ad = params[:ad].gsub(“\n”, “
”)
@ad.title = params[:title]
if @ad.save
@ad.handle_images(params[“image_attachments”])
flash[:notice] = “ad updated”
else
flash[:warning] = “error updating ad”
end
redirect_to :controller => ‘ads’, :action =>
‘manage’, :activation_code => @ad.activation_code
end
end
def feed
@ads = Ad.all_active
respond_to do |format|
format.rss { render :layout => false }
format.atom # index.atom.builder
end
end
end
Take care!
– Rubienubie