Hi all,
I coded a method named "place_ads" that I have in my Ad Controller that I would like to call each time my "ads.rhtml" or "user.rhtml" layout is displayed but not when my "admin.rhtml" is displayed.
Should I put the method in the Application Controller and use a before filter? Is there a way to exclude the views in the Admin Controller from calling the method then. Or would I be better off leaving the code in the Ad controller and somehow calling it from the layout?
INARD (I am not a Rails Developer, ahh..... yet), but couldn't you put in your view:
<%= render :partial => "ads/place_ad.rhtml" if !@user.admin? %>
This would also require a "admin?" method on the user, but that should be easy.
Here is my code (haven't got around to writing a function yet):
class AdController < ApplicationController layout "ads"
def place_ads
#Creates an ordered array @ads = Ad.find(:all, :order => "rank", :conditions => "locked_slot = 0")
@slot1 = Ad.find(:first, :conditions => "locked_slot = 1") if @slot1 @ad1 = @slot1 else @ad1 = @ads.shift @ads.push(@ad1) end @ad1.times_displayed = @ad1.times_displayed + 1 @ad1.save
This code repeats a lot (as the previous poster said) - why not convert the @slots to an array and loop/inject over @ads to populate it? This would also require refactoring of the view - I would suggest passing the @slots to a partial as a collection.