# Really simple question - how do I call information from the

**URL:** <https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837>\
**Category:** Rails\
**Created:** [March 3, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837 "2008-03-03T20:02:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 3, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/1 "2008-03-03T20:02:07Z")

</div>

I know how to create controllers and models. However what I do not know  
is how to call information into the front end of the site - particularly  
I do not know how to even place the graphic design into the site.

I imagine I use the views. Ok, so I do that. Do I do ruby  
script/generate controller index?  
I have seen tutorials where the index is defined within a controller so  
I am not 100% sure on this.

To help us out, essentially I want one page that serves the entire site  
(the index page). Everything will be called within that - so I assume  
the index view would be key. I will also be adding code to have a SEO  
friendly URL for each page that is requested. - Thats another topic so  
whatever.

So how do I go about having a producting index page that calls the data  
that I am using in the admin (scaffolded) pages?

Another thing that is throwing me off is the public folder - how does  
that come into play with the controllers and views?

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 3, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/2 "2008-03-03T20:30:34Z")

</div>

Layouts wrap your views. When you scaffolded, you should have gotten a  
layout file in the layouts folder. Rename that to application.html.erb  
and  
it will be the default template for all pages.

Next, the public folder is for your index page. However, things in the  
public folder don’t invoke Rails, so you won’t get to share the layout  
you  
just made.

typically, what we do is delete public/index.html. Then we make a new  
controller

ruby script/generate controller public index

This creates a public controller and an index view. It will use the  
application layout as long as you have one.

Then, take a look at config/routes.rb to specifiy that the main URL is  
/public/index.

map.root :controller =\> “public”, :action=\>“index”

(something similar is already commented out.)

For more about the public folder, see “Page caching”. Does that help?

On Mon, Mar 3, 2008 at 1:02 PM, Joel S. \<

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 3, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/3 "2008-03-03T20:41:17Z")

</div>

or use a redirect script in the public/index.html page

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 4, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/4 "2008-03-04T15:36:07Z")

</div>

On Mar 4, 2008, at 8:18 AM, Joel S. wrote:

> > public folder don’t invoke Rails, so you won’t get to share the  
> > Ok I am with you so far. One question I have though is how to call  
> > multiple items into a single page - this might be a different topic  
> > all  
> > together?
> 
> Say my index page is suppose to have its main content in the center, a  
> username login / password option on the top, and cateogries on the top  
> and the left. Each of the just mentioned have their own controllers  
> and  
> models since they are all their own tables in the db.

(I’m jumping into the middle of this thread so apologies if I’m  
repeating something already said.)

Your implicit assumption that each table must be handled by its own  
controller is false.

The login option can be a partial that is conditionally rendered  
(assuming the conventions of acts\_as\_authenticated):

\<% if logged\_in? -%\>

\<%= current\_user.name %\>

\<% else -%\> \<%= render :partial =\> 'login' %\> \<% end -%\>

For the categories, the controller sets up an instance variable by  
invoking the model:

@top\_categories = Category.for\_top  
@side\_categories= Category.for\_side(:kind =\> ‘interesting’)

which the layout or the view can render as a partial

\<%= render :partial =\> ‘top\_categories’, :object =\> @top\_categories %\>  
\<%= render :partial =\> ‘side\_category’, :collection =\>  
@side\_categories %\>

If these partials are used by more than one controller, you can place  
them all into an app/views/shared/ directory or refer to them with  
their native controller’s name:  
\<%= render :partial =\> ‘account/login’ %\>

> So how do I include it all together? I was told in order to have the  
> visual layout in my front end to put the layout in the layouts folder  
> for say the index page (or the gloabl layout). Well thats peachy but I  
> am not sure how to call all of the different models into one page  
> using  
> the

In the layout:

\<%= yield :this\_thing %\>

In your view:

\<% content\_for :this\_thing do %\>

stuff

  
\<%= link\_to ‘wow’, wow\_path, :class =\> ‘amazing’ %\>  
\<% end %\>

The view is actually rendered first and then the layout is found and  
wrapped around it even though you tend to think of it the other way  
around.

> Should I be thinking of this as having a layout for each controller  
> then  
> calling all of those seperate layouts into one?

You certainly can have a different layout per controller (which is the  
default actually if you follow the naming conventions), but you can  
also specify a layout directly:

```
render :layout => 'alternate'
render :action => 'edit', :layout => 'admin'

```

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 6, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/5 "2008-03-06T14:32:02Z")

</div>

Rob B. wrote:

> Your implicit assumption that each table must be handled by its own  
> controller is false…

Ok actually imagine i quoted the entire thread you wrote - I thought it  
was too long to quote here.

Let me just run my mind on this and confirm with me that my assumptions  
based on what you all said is correct.

So I can (and will have to for my needs) remove index.html from the  
public folder. Instead I will generate a public index. I am assuming  
here that the keyword public within that command will create the index  
in the public directory?

This is actually a problem I am considering. Essentially the entire site  
will be using one graphic template that is being created. If I am  
understanding this correctly, I will be placing the graphic design  
pieces into the layout. The visual aspect is going to be pretty much all  
stylesheet driven and html tables to place the css design accordingly.  
So I can simply put the \<% yield :something %\> where approriate within  
the html tables. Am I going the right direction here so far?

That leads to my next question, what about stylesheets? can they be used  
within layouts?

As far as everything else goes it seems pretty straight forward at the  
moment. I cannot wrap my head around act\_as\_authenticated but I need to  
worry about the above first.

Thanks for all of your help so far

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 4, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/6 "2008-03-04T14:18:23Z")

</div>

Brian H. wrote:

> Layouts wrap your views. When you scaffolded, you should have gotten a  
> layout file in the layouts folder. Rename that to application.html.erb  
> and  
> it will be the default template for all pages.
> 
> Next, the public folder is for your index page. However, things in the  
> public folder don’t invoke Rails, so you won’t get to share the layout  
> you  
> just made.
> 
> typically, what we do is delete public/index.html. Then we make a new  
> controller
> 
> On Mon, Mar 3, 2008 at 1:02 PM, Joel S. \<

Ok I am with you so far. One question I have though is how to call  
multiple items into a single page - this might be a different topic all  
together?

Say my index page is suppose to have its main content in the center, a  
username login / password option on the top, and cateogries on the top  
and the left. Each of the just mentioned have their own controllers and  
models since they are all their own tables in the db.

So how do I include it all together? I was told in order to have the  
visual layout in my front end to put the layout in the layouts folder  
for say the index page (or the gloabl layout). Well thats peachy but I  
am not sure how to call all of the different models into one page using  
the

Should I be thinking of this as having a layout for each controller then  
calling all of those seperate layouts into one?

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 7, 2008, 1:28am UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/7 "2008-03-07T01:28:42Z")

</div>

Actually, I just found out that even when you change the route so that I  
had my index page become a public controller I was still able to use the  
style sheets and images from the public directory.

Could anyone enlighten me as to why it is happening this way? I would  
have thought that be routing to a different location would pretty much  
void any use of the public directory.

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 7, 2008, 3:34am UTC](https://www.ruby-forum.com/t/really-simple-question-how-do-i-call-information-from-the/130837/8 "2008-03-07T03:34:46Z")

</div>

On Mar 6, 2008, at 4:28 PM, Joel S. wrote:

> –  
> What looks like [yourdomain.com/](http://yourdomain.com/) to the browser looks like /some/path/  
> to/your/app/public on your server. Put differently, /public is like  
> your DocumentRoot. All your static files are served out of that  
> document root, including images, javascripts and stylesheets (well,  
> you can serve some or all of them dynamically, but that’s a different  
> story for a different time).

What routing affects is how requests that cannot be fulfilled by  
serving a static page are parsed. So, say the browser does a GET for  
[yourdomain.com/](http://yourdomain.com/) how does that work? First, the server (Apache or  
mongrel, maybe) looks for a default file in public. The default names  
commonly include “index.html” so if there’s an “index.html” there in  
the root of your public folder, it is served and story told.

If there is no static file found, then the dispatcher is invoked to  
chase through the routes looking for something that could fulfill the  
request. In this case, it would be a fallback route if you’ve  
specified it.

So, when you changed your fallback route to point to a dynamically  
generated page, that doesn’t mean that the GET requests for  
[yourdomain.com is available for purchase - Sedo.com](http://yourdomain.com/prototype.js) and [yourdomain.com/scaffold.css](http://yourdomain.com/scaffold.css) or  
whatever aren’t still served statically. That’s the behavior you want:  
Serve statically, if possible. Generate pages dynamically when  
necessary.

HTH
