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.
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:
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
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
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,
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.
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.
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.
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.