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 -%>

Hey, I'm a Heading!

<% end -%>
  • I'm a list
  • </ul

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

    Example <%= yield :heading %>

    where's the list

    <%= yield %>

    Will generate:

    Example

    Hey, I'm a Heading!

    where's the list

    • I'm a list
    • </ul

      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:

On Jan 28, 6:00 pm, Rob B. [email protected]

Amen!

On Jan 29, 1:12 am, Rob B. [email protected]

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
a :conditions clause on a finder in a controller, go to the model and
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

On Jan 29, 2008, at 12:50 AM, tresero wrote:

</ul

    Is there a way to include the pages controller in the main

    Is that what you’re trying to do?

    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.

Rob B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739
Skype: rob.biedenharn

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

On Jan 28, 10:12 pm, Rob B. [email protected]

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

On Jan 29, 8:53 am, Rob B. [email protected]

On Jan 29, 2008, at 11:28 AM, tresero wrote:

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

In your controllers, avoid the temptation to put too much code and
every bit of your code that isn’t so trivial that it couldn’t

part fails, for obvious reasons. I need a way to get the pages

<% end -%>

On Jan 28, 3:39 pm, tresero [email protected] wrote:

in the which case you would use:

in the application layout.
I hope this makes sense.

Rob B. http://agileconsultingllc.com
[email protected]

Excellent article! Thanks for sharing.

–Alan

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

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