Solved

Using a partial render across all pages in a app

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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