# Building main page with admin controller

**URL:** <https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187>\
**Category:** Rails\
**Created:** [January 24, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187 "2010-01-24T17:18:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 24, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/1 "2010-01-24T17:18:45Z")

</div>

I am trying to build a main page with a customized view containing login  
control and several 'div with menus and buttons all combined into one  
page. I believe I have to use admin controller. Is there an article that  
shows how to do this?

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 24, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/2 "2010-01-24T17:53:55Z")

</div>

I am describing what I am trying to accomplish:  
I want to create a main welcome page. So I ran script/generate  
admin/welcome. This created welcome controller  
Then I wanted to attach this welcome controller to the main  
application.html.erb  
In the application.html.erb file I want to show a â€˜loginâ€™ control and  
two action buttons: â€˜suppliersâ€™ and â€˜contractorsâ€™. This means somehow I  
have to create three controllers: â€˜loginâ€™, â€˜suppliersâ€™, and  
â€˜contractorsâ€™.

- Should I create all three controllers under admin?
- How to connect my welcome controller to these three controllers and  
get to display the application.html.erb file  
I do not know if I am thinking in the correct direction also? Please  
help?

Vishwa R. wrote:

> I am trying to build a main page with a customized view containing login  
> control and several 'div with menus and buttons all combined into one  
> page. I believe I have to use admin controller. Is there an article that  
> shows how to do this?

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 24, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/3 "2010-01-24T18:24:59Z")

</div>

You are going to have to start simple and understand the mechanics of  
rails before you continue any further.

Your first item of the day should be learning more about rails:

> **[Ruby on Rails Guides](https://guides.rubyonrails.org/)**
>
> Ruby on Rails Guides

As for your question about how layouts work with rails… Inside of  
your app, the layouts folder contains specific layouts which act as  
wrappers for your content.

If you just want to make it simple for starters, remove all of the  
layouts “except” application.html.erb which will be your main wrapper  
layout file.

Inside of this file you would place something similar:

> <https://gist.github.com/anonymous/285321>

The yield is where your content will go from your views. This is a very  
bare bones idea of a layout I’m showing. It’s just enough to get you  
started though. You’ll have to go visit [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)  
in order to read more about rails and what you want to accomplish.

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 24, 2010, 10:21pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/4 "2010-01-24T22:21:01Z")

</div>

OK. I got the welcome page working to look at application.html.erb file  
by creating a controller ‘home’ and updating the routes.rb with a line  
map.root :controller =\> “home”  
Now, if I customize home/index.html.erb then I can display whatever I  
need to - gets called at \<%= yield %\>, I hope - Is this the right  
approach?

I still trying to understand yield(:head) and yield(:title) - can you  
pl. tell me?

Vishwa R. wrote:

> Thank you. I am new and sorry for asking complicated questions, without  
> reading.
> 
> I copied the code you suggested in application.html.erb file in  
> view/layouts dir in my project.  
> When I start the server and say http:localhost/3000 it shows default  
> ruby welcome page.
> 
> - How to I display contents of application.html.erb file.
> - Also what is yield(:head} and yield(:title)
> 
> When I was looking at other exmaples, they all create a model and then  
> change the model.html.erb file to display as default view. But for my  
> welcome page I don’t have a model or a controller? So how to invoke the  
> application.html.erb file
> 
> Alpha B. wrote:
> 
> > You are going to have to start simple and understand the mechanics of  
> > rails before you continue any further.
> > 
> > Your first item of the day should be learning more about rails:
> > 
> > [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)
> > 
> > As for your question about how layouts work with rails… Inside of  
> > your app, the layouts folder contains specific layouts which act as  
> > wrappers for your content.
> > 
> > If you just want to make it simple for starters, remove all of the  
> > layouts “except” application.html.erb which will be your main wrapper  
> > layout file.
> > 
> > Inside of this file you would place something similar:
> > 
> > [application.html.erb · GitHub](http://gist.github.com/285321)
> > 
> > The yield is where your content will go from your views. This is a very  
> > bare bones idea of a layout I’m showing. It’s just enough to get you  
> > started though. You’ll have to go visit [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)  
> > in order to read more about rails and what you want to accomplish.

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 24, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/5 "2010-01-24T21:29:16Z")

</div>

Thank you. I am new and sorry for asking complicated questions, without  
reading.

I copied the code you suggested in application.html.erb file in  
view/layouts dir in my project.  
When I start the server and say http:localhost/3000 it shows default  
ruby welcome page.

- How to I display contents of application.html.erb file.
- Also what is yield(:head} and yield(:title)

When I was looking at other exmaples, they all create a model and then  
change the model.html.erb file to display as default view. But for my  
welcome page I don’t have a model or a controller? So how to invoke the  
application.html.erb file

Alpha B. wrote:

> You are going to have to start simple and understand the mechanics of  
> rails before you continue any further.
> 
> Your first item of the day should be learning more about rails:
> 
> [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)
> 
> As for your question about how layouts work with rails… Inside of  
> your app, the layouts folder contains specific layouts which act as  
> wrappers for your content.
> 
> If you just want to make it simple for starters, remove all of the  
> layouts “except” application.html.erb which will be your main wrapper  
> layout file.
> 
> Inside of this file you would place something similar:
> 
> [application.html.erb · GitHub](http://gist.github.com/285321)
> 
> The yield is where your content will go from your views. This is a very  
> bare bones idea of a layout I’m showing. It’s just enough to get you  
> started though. You’ll have to go visit [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)  
> in order to read more about rails and what you want to accomplish.

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 25, 2010, 12:25am UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/6 "2010-01-25T00:25:00Z")

</div>

\<% title(“Administration Page” %\>

should read…

\<% title(“Administration Page”) %\>

My apologies.

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 25, 2010, 12:23am UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/7 "2010-01-25T00:23:39Z")

</div>

Vishwa R. wrote:

> OK. I got the welcome page working to look at application.html.erb file  
> by creating a controller ‘home’ and updating the routes.rb with a line  
> map.root :controller =\> “home”  
> Now, if I customize home/index.html.erb then I can display whatever I  
> need to - gets called at \<%= yield %\>, I hope - Is this the right  
> approach?
> 
> I still trying to understand yield(:head) and yield(:title) - can you  
> pl. tell me?

The idea behind yield flows with a method called content\_for().

In your application\_helper.rb file in the helpers folder place the  
following:

def title(page\_title)  
content\_for(:title) { page\_title }  
end

Now then, let’s say you have a title in your layout that shows this:

\<%= "My Home Page" || yield(:title) %\>

On every single page that a person opens up on your site that uses the  
layout wrapper, it would show the same exact title of “My Home Page”.  
You wouldn’t want the same title for every page would you? No, probably  
not.

So, with the helper file I just showed you how to make you could do the  
following:

Let’s say you have a new page called index.html.erb for a new controller  
called administration. So inside the index.html.erb you could place at  
the top of that file the following line:

\<% title(“Administration Page” %\>

Now then, whenever that pages loads it will automatically see that you  
are calling a content\_for method for title(“some label”) and the helper  
file will place the new title there instead of the old one. So, on the  
admin page, you will see Administration Page and not My Home Page.

This allows you to decide what type of content to use on different  
pages.

Let’s add another helper for the head.

def head(css\_file)  
content\_for(:head) { stylesheet\_link\_tag “#{css\_file}” }  
end

Again, using this example, you might want to specifically overwrite your  
css with some specific css file that should only be used on one or two  
pages on your site. On these pages within your views, you could do:

\<% head(‘my\_custom\_css’) %\>

Which will be yielded where?.. That’s right, in the head section where  
you wanted it to go. It’s the same thing as placing a  
stylesheet\_link\_tag ‘my\_custom\_css’ in the head of that page only.

Yielding content\_for elements in specific places allows you to define  
specific content to appear in certain places of the layout, but only on  
certain pages.

I hope that helps you understand that a little bit. I would try it out  
on several pages and see how it works.

Please keep in mind that I’m not advocating loading multiple css files.  
But, there are times you might require multiple css files on a specific  
page. So, I’m just supplying you with a basic understanding of how to  
use helpers to define content\_for methods on specific pages which are  
included within your layout only for those specific pages.

I hope I made things clear enough for you.

Take care.

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [January 25, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/building-main-page-with-admin-controller/182187/8 "2010-01-25T22:18:06Z")

</div>

Thanks a lot for your help!

Alpha B. wrote:

> \<% title(“Administration Page” %\>
> 
> should read…
> 
> \<% title(“Administration Page”) %\>
> 
> My apologies.
