Hey Everyone,
At the bottom of my site layout I have a suggested item box. This box
is at the bottom of everypage but the admin/manage section.
I tried using:
class ApplicationController < ActionController::Base
@mightlike = Vehicle.random
end
then in the view for vehicles:
// vehicles.html.erb
<dt>Make:</dt><dd><%=h @mightlike.make %></dd>
It works fine if i add "@mightlike = Vehicle.random" in:
class VehiclesController < ApplicationController
def index
@vehicles = Vehicle.find_all_by_market_status(''available'')
@mightlike = Vehicle.random
end
end
But that restricts it to the index on the vehicle controller and I
need it in almost every view being controlled by every controller.
Whats the best way to do that?
Try a before_filter. In your ApplicationController add the following:
before_filter :load_mightlike
private
def load_mightlike
@mightlike = Vehicle.random
end
Darian Shimy
--
http://www.darianshimy.com
http://twitter.com/dshimy
On Mon, Nov 16, 2009 at 10:49 PM, brianp
<brian.o.pearce-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Hey Everyone,
>
> At the bottom of my site layout I have a suggested item box. This box
> is at the bottom of everypage but the admin/manage section.
>
> I tried using:
>
> class ApplicationController < ActionController::Base
> @mightlike = Vehicle.random
> end
>
> then in the view for vehicles:
>
> // vehicles.html.erb
> <dt>Make:</dt><dd><%=h @mightlike.make
%></dd>
>
> It works fine if i add "@mightlike = Vehicle.random" in:
>
> class VehiclesController < ApplicationController
> def index
> @vehicles =
Vehicle.find_all_by_market_status(''available'')
> @mightlike = Vehicle.random
> end
> end
>
> But that restricts it to the index on the vehicle controller and I
> need it in almost every view being controlled by every controller.
> Whats the best way to do that?
> >
worked perfect, thank you kindly =) On Nov 16, 11:27 pm, Darian Shimy <dsh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try a before_filter. In your ApplicationController add the following: > > before_filter :load_mightlike > > private > def load_mightlike > @mightlike = Vehicle.random > end > > Darian Shimy > --http://www.darianshimy.comhttp://twitter.com/dshimy > > On Mon, Nov 16, 2009 at 10:49 PM, brianp <brian.o.pea...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey Everyone, > > > At the bottom of my site layout I have a suggested item box. This box > > is at the bottom of everypage but the admin/manage section. > > > I tried using: > > > class ApplicationController < ActionController::Base > > @mightlike = Vehicle.random > > end > > > then in the view for vehicles: > > > // vehicles.html.erb > > <dt>Make:</dt><dd><%=h @mightlike.make %></dd> > > > It works fine if i add "@mightlike = Vehicle.random" in: > > > class VehiclesController < ApplicationController > > def index > > @vehicles = Vehicle.find_all_by_market_status(''available'') > > @mightlike = Vehicle.random > > end > > end > > > But that restricts it to the index on the vehicle controller and I > > need it in almost every view being controlled by every controller. > > Whats the best way to do that?