[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Using a partial render across all pages in a app

Posted on 2011-03-09
3
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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