View question about yield

I am currently in a brain freeze.
I would like to display my pages (dynamically in cms, grabbing title),
in the application layout.
The yield is: In HAML
= yield :sidemenu

The problem is that this yield is in the pages MVC. Is there a way to
have a view instantiate another view?
Obviously the menu only shows when the url is mysite/pages/ since
application.html.haml does not know about pages MVC.

I hope this makes sense.

Not exactly sure what you're referring to when you say "in the pages
MVC". However, it sounds like you might be trying to render a partial,
in the which case you would use:

render :partial => "filename.rhtml"

Is that what you're trying to do?

It is not a partial, in the application view I am calling:
= yield :sidemenu

sidemenu is defined in pages/show.rhtml

I can't use a partial in a view can I?

I tried this, but it does not use the pages controller, so it errors
out.
Is there a way to include the pages controller in the main
application?

Let me phrase this another way, how can I use data from a specific
controller in an application view. I don't see something like include
pages which would gather the pages and pass them to the
application.rhtml

In your pages/show.rhtml
<% content_for :heading do -%>
   <h1>Hey, I'm a Heading!</h1>
<% end -%>

<ul>
   <li>I'm a list</li>
</ul

Then in application.rhtml (or whatever file you call out for the layout):

<html>
<head><title>Example</title></head>
<body>
<%= yield :heading %>
<p>where's the list</p>
<%= yield %>
</body>
</html>

Will generate:

<html>
<head><title>Example</title></head>
<body>
   <h1>Hey, I'm a Heading!</h1>
<p>where's the list</p>

<ul>
   <li>I'm a list</li>
</ul
</body>
</html>

Is that what you're trying to understand?

-Rob

I get that part, the problem is how to use a model in the "main"
template. The sidebar does a query to pull all page titles and display
them on the main template.
The main template does not know how to deal with pages. Therefor the:
- for item in @pages
part fails, for obvious reasons. I need a way to get the pages model/
controller logic into the main template.
Maybe there is no way, I can alway do raw sql, but I am trying to do
it the rails way :wink:

I'm not sure I understand what you're really trying to do, but let me
throw this out to you:

In your views, avoid having "code". Use the instance variables set by
the controller and try to do nothing more than simple tests and loops
(@things.each or render :partial => '...', :collection => @things)

In your controllers, avoid the temptation to put too much code and
don't (DON'T) put any SQL in the controller. If you find yourself
putting HTML into a controller method, you probably should be in the
view or at least in a helper.

In your models, do the heavy lifting, but rest the weight on
ActiveRecord associations where you can. Whenever you want to put
put a new method that does exactly what you need.

You were writing tests, right? Seriously, try to have a test for
every bit of your code that isn't so trivial that it couldn't possibly
break. (Take careful note that the definitions of "trivial" and
"...couldn't possibly..." will vary over time according to the balance
between fear and experience -- pay attention to the feedback loop!)

-Rob

Amen!

Rob,
Thanks for the info, but this is not testing.
Let me see if I can explain it another way with a different scenario
(and I could be doing this completely wrong in my thinking).
I have a site, one component is a blog, so there is a blog model,
controller and view. I also have an application view which is the main
view on the whole site.
If I wanted to get a random blog headline on the site, how would I go
about doing it? If I am not on a blog url, application does not know
how to get at the data.
So I hope that makes more sense. I am thinking I need to do some query
in controllers/application.rb. I was hoping to make it DRY and not
repeat the code from blog.rb.

Thanks
Jon

Rob,
Thanks for the info, but this is not testing.
Let me see if I can explain it another way with a different scenario
(and I could be doing this completely wrong in my thinking).
I have a site, one component is a blog, so there is a blog model,

...So there's a Blog.headlines that returns an array of headlines (or Blog.headlines(n) to get the most recent n headlines)

controller and view. I also have an application view which is the main
view on the whole site.
If I wanted to get a random blog headline on the site, how would I go
about doing it? If I am not on a blog url, application does not know
how to get at the data.

That controller calls Blog.random_headline (possibly in a before_filter) and the layout incorporates the view for the headline.

So I hope that makes more sense. I am thinking I need to do some query
in controllers/application.rb. I was hoping to make it DRY and not
repeat the code from blog.rb.

Thanks
Jon

It that becoming clear?
-Rob

Yes, I believe so. I guess I have to put this in the controllers/
application.rb.
I will look at it tonight and let you know.

Thanks

Tresero,

Didn't have time to read thoroughly, but it seems this paper could
help a lot:

http://www.railsdev.ws/blog/wp-content/uploads/2007/10/modular_page_assembly_in_rails.pdf

Cheers, Sazima

Excellent article! Thanks for sharing.

–Alan

OK, I hate to open this backup but I am still stuck.
Let me explain again.
I have a template for the entire site, it is called
application.html.haml (I am using haml).
I have a controller called pages which is a mini-cms and is modeled
after vaporbase.
I want to have on the left side of my application layout the page
titles and children.
In my application.html.haml I have the following line:
   = render(:partial => "pages/sidemenu.html.haml", :locals =>{:pages
=> @pages} )

pages/sidemenu.html.haml is:
  %h3 Pages
  %ul{:id=>"leftmenu", :class=>"rMenu-wide rMenu-ver rMenu"}
    - for item in @pages
      = link_to item.title, page_url(:id => item.name)

If I am in the url /pages/ everything renders correctly.
Outside of /pages/, I get the following error:
   You have a nil object when you didn't expect it!
   You might have expected an instance of Array.
   The error occurred while evaluating nil.each
This is related to not seeing an instance var named pages.

How can I use the data from pages site-wide? I know I am missing
something obvious.

Thanks

OK, I hate to open this backup but I am still stuck.
Let me explain again.
I have a template for the entire site, it is called
application.html.haml (I am using haml).
I have a controller called pages which is a mini-cms and is modeled
after vaporbase.
I want to have on the left side of my application layout the page
titles and children.
In my application.html.haml I have the following line:
   = render(:partial => "pages/sidemenu.html.haml", :locals =>{:pages
=> @pages} )

pages/sidemenu.html.haml is:
  %h3 Pages
  %ul{:id=>"leftmenu", :class=>"rMenu-wide rMenu-ver rMenu"}
    - for item in @pages
      = link_to item.title, page_url(:id => item.name)

If I am in the url /pages/ everything renders correctly.
Outside of /pages/, I get the following error:
   You have a nil object when you didn't expect it!
   You might have expected an instance of Array.
   The error occurred while evaluating nil.each
This is related to not seeing an instance var named pages.

How can I use the data from pages site-wide? I know I am missing
something obvious.

Thanks