Solved

Using a partial render across all pages in a app

Posted on 2011-03-09
3
245 Views
Last Modified: 2012-05-11
I have written a test app with a Controller called pages. Pages has index, about, contact, and doc in the Pages controller. I want to create a widget that is a partial render that will be included in all of them with the default layout. The widget will display data from a model. I got this to work by creating a variable in each method in the Pages controller that are all the same thing. Howdo I do this without duplication all that effort?

For example in my Pages controller I have:
class PagesController < ApplicationController
  
  def index
    @blah="42"
  end

  def contact
     @blah="42"
  end

  def about
     @blah="42"
  end

  def doc
     @blah="42"
  end
end

Open in new window


I want to provide the data to the partial without having to repeat it everywhere.
0
Comment
Question by:LinuxInAtlanta
3 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 500 total points
ID: 35094292
Check out ActionController::Base filters:

class PagesController < ApplicationController
  # This would call :set_categories before calling corresponding
  # action-specific methods.
  before_filter :set_categories

  def action_a
  end

  def action_b
  end

  protected

  def set_categories
    @categories = Category.find(:all)
  end
end

Open in new window


If you don't want the method to be called for all actions, you can specify :only or :except options to the before_filter call.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 36433940
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question