# Question about Erb

**URL:** <https://www.ruby-forum.com/t/question-about-erb/131095>\
**Category:** Rails\
**Created:** [March 6, 2008, 12:06am UTC](https://www.ruby-forum.com/t/question-about-erb/131095 "2008-03-06T00:06:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 6, 2008, 12:06am UTC](https://www.ruby-forum.com/t/question-about-erb/131095/1 "2008-03-06T00:06:52Z")

</div>

Okay, so this is really a beginner’s question. I’m just starting to  
move over from ColdFusion to Rails and I’m having some trouble  
accessing ActiveRecord objects from within views.

I’m sure I’m missing something pretty obvious. I have a page model, a  
pages\_controller, a content model and a content\_controller. The idea  
is for the content controller to manage the administrator’s  
interaction with the content and for pages to display the content to  
visitors.

But I’m having problems referencing the variables inside the  
pages\_controller. I have:

def show  
@page=Page.find(params[:id])  
@content = Content.find(:all)  
end

I have a partial view \_header.rhtml that just has:

\<% for page in @pages %\>

- \<%=@page.title%\>

\<%end%\>

There are two problems: 1) It lists only the page currently being  
shown (probably because of the @page=Page.find(params[:id]) line–I  
need to figure out a way to list all of the pages and 2) Although I’ve  
established the relationship between content and pages in their  
respective model pages, I need a way of referencing only the content  
associated with the page being viewed.

I know its basic, but if anyone could throw out some help, I’d  
appreciate it.

Ron

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 6, 2008, 1:11am UTC](https://www.ruby-forum.com/t/question-about-erb/131095/2 "2008-03-06T01:11:56Z")

</div>

Ron wrote:

> But I’m having problems referencing the variables inside the
> 
> I know its basic, but if anyone could throw out some help, I’d  
> appreciate it.
> 
> Ron

@pages is nil in your view because you didn’t define in in the  
controller action, you only defined @page. If you define @pages  
(possibly with something like @pages = Page.find(:all)) you’ll have more  
luck.

And as a seperate observation… you might want to just make ‘content’ a  
text column on the pages table, and call it with @page.content, instead  
of making it an entire different class.

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 6, 2008, 1:34am UTC](https://www.ruby-forum.com/t/question-about-erb/131095/3 "2008-03-06T01:34:47Z")

</div>

Hi Jon,

Thanks! That’s helpful. I guess my thought behind making pages a  
separate controller is that I wanted to be able to login at some point  
down the road and edit the content of pages. For some reason, I got  
it in my head that it would be better to have a separate controller  
for the presentation part and another for the content part.

I guess if I had just one controller, I’d just change the controller  
to require authorization to edit?

Ron

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 6, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/question-about-erb/131095/4 "2008-03-06T17:24:17Z")

</div>

Depending on your implementation, you may want to do separate  
controllers, or maybe not. However, that wouldn’t change how you setup  
your models. It’s a common misconception I see with people just getting  
into Rails (I even had it myself once) that there is a 1 to 1  
correlation to models and controllers. It’s not uncommon in complex  
Rails apps however to have some controllers deal with multiple models in  
a single action and to have several controllers that all share one model  
but do different things with it.

Ron wrote:

> > > I’m sure I’m missing something pretty obvious. I have a page model, a  
> > > @content = Content.find(:all)  
> > > There are two problems: 1) It lists only the page currently being

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 6, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/question-about-erb/131095/5 "2008-03-06T23:33:05Z")

</div>

Thanks Jon! That really helps.

Ron
