?
Solved

Using a partial render across all pages in a app

Posted on 2011-03-09
3
Medium Priority
?
248 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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