Sean Lerner
2006-Nov-22 20:46 UTC
Newbie Q: Making code accessible by controller regardless of action
Hello, I have a model: -------------------------------------------------------- review.rb -------------------------------------------------------- class Review < ActiveRecord::Base def self.published find(:all, :conditions => "she_published = TRUE and he_published = TRUE and date_to_be_published <= now()") end end -------------------------------------------------------- And a controller: -------------------------------------------------------- display_controller.rb -------------------------------------------------------- class DisplayController < ApplicationController def index @published_reviews = Review.published end def show @review = Review.find(params[:id]) @published_reviews = Review.published end def about @published_reviews = Review.published end end -------------------------------------------------------- And part of my layout: -------------------------------------------------------- display.rhtml -------------------------------------------------------- <% for review in @published_reviews %> <%= link_to review.name, :controller => "display", :action => "show", :id => review.id %> <br /> <% end %> -------------------------------------------------------- Right now the only way I can get things to work is to put the line @published_reviews = Review.published in each def Is there a place I can put this line just once so it''s accessible by the layout, no matter what the action? Thanks, Sean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jdswift
2006-Nov-22 22:20 UTC
Re: Newbie Q: Making code accessible by controller regardless of action
before_filter :get_published_reviews def get_published_review @published_reviews = Review.published end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jdswift
2006-Nov-22 22:24 UTC
Re: Newbie Q: Making code accessible by controller regardless of action
oops, typo ...def get_published_reviews... If you put that at the top of a controller it will be available in all the actions (if you put it in the application controller it will be available to all actions in all controllers). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sean Lerner
2006-Nov-30 17:10 UTC
Re: Newbie Q: Making code accessible by controller regardless of action
thanks for your help with this. this worked for me. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---