Solved

Using a partial render across all pages in a app

Posted on 2011-03-09
3
243 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now