Question about Erb

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 %>
  <ul>
    <li><%=@page.title%></li>
  </ul>
<%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

Ron wrote:

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 %>
  <ul>
    <li><%=@page.title%></li>
  </ul>
<%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

@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.

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

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:

Thanks Jon! That really helps.

Ron