# How to generate a layout for a controller

**URL:** <https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824>\
**Category:** Rails\
**Created:** [December 17, 2008, 9:37am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824 "2008-12-17T09:37:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 9:37am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/1 "2008-12-17T09:37:07Z")

</div>

I use ruby script/generate controller mycontroller to create a  
controller. There app/views/mycontroller/\* include all the view files.  
But I didn’t find there is any layout files associated with these views.  
How can I generate a layout for these views?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 9:43am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/2 "2008-12-17T09:43:46Z")

</div>

On Wed, Dec 17, 2008 at 9:37 AM, Zhao Yi  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I use ruby script/generate controller mycontroller to create a  
> controller. There app/views/mycontroller/\* include all the view files.  
> But I didn’t find there is any layout files associated with these views.  
> How can I generate a layout for these views?

Create the layout under app/views/layouts and depending on your  
version of rails:

1.x - mycontroller.rhtml  
2.x - mycontroller.html.erb

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 9:50am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/3 "2008-12-17T09:50:09Z")

</div>

Franz S. wrote:

> On Wed, Dec 17, 2008 at 9:37 AM, Zhao Yi  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I use ruby script/generate controller mycontroller to create a  
> > controller. There app/views/mycontroller/\* include all the view files.  
> > But I didn’t find there is any layout files associated with these views.  
> > How can I generate a layout for these views?
> 
> Create the layout under app/views/layouts and depending on your  
> version of rails:
> 
> 1.x - mycontroller.rhtml  
> 2.x - mycontroller.html.erb

If I create this layout file manually, how will rails link it to the  
views? Which URL refer to this layout?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 9:56am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/4 "2008-12-17T09:56:06Z")

</div>

Hi…,

It will take automatically.

app/view/layout/

Here layout folder is meant for layouts

For any controller you can use layout by naming layout file as same name  
as  
controller…

Regards  
hafeez

On Wed, Dec 17, 2008 at 2:20 PM, Zhao Yi

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/5 "2008-12-17T17:59:57Z")

</div>

Or you can set layout directly in your controller:

class MyController \< ApplicationController  
layout ‘some\_other\_layout’  
end

Regards,  
Bosko

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/6 "2008-12-17T16:18:26Z")

</div>

> If I create this layout file manually, how will rails link it to the  
> views? Which URL refer to this layout?

You can specify which layout to use in your controller.

layout ‘main’

That would use app/views/layouts/main.html.erb as the layout. You can  
specify a default global layout by placing this in the application  
controller.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 18, 2008, 2:47am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/7 "2008-12-18T02:47:12Z")

</div>

> I set this in my controller and which link I should use for this layout.  
> When I invoke the controller in the browser, it will refer to the index  
> page not the layout page.

Give this a read - it explains layouts and how to use them better than  
I can here.

> **[Ruby on Rails - Layouts](http://www.tutorialspoint.com/ruby-on-rails/rails-layouts.htm)**
>
> Ruby on Rails Layouts - A layout defines the surroundings of an HTML page. It's the place to define a common look and feel of your final output. Layout files reside in app/views/layouts.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 18, 2008, 1:52am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/8 "2008-12-18T01:52:13Z")

</div>

Bosko wrote:

> Or you can set layout directly in your controller:
> 
> class MyController \< ApplicationController  
> layout ‘some\_other\_layout’  
> end
> 
> Regards,  
> Bosko  
> Hi,

I set this in my controller and which link I should use for this layout.  
When I invoke the controller in the browser, it will refer to the index  
page not the layout page.

Thanks,

Zhao Yi

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 18, 2008, 2:49am UTC](https://www.ruby-forum.com/t/how-to-generate-a-layout-for-a-controller/155824/9 "2008-12-18T02:49:50Z")

</div>

John Y. wrote:

> > I set this in my controller and which link I should use for this layout.  
> > When I invoke the controller in the browser, it will refer to the index  
> > page not the layout page.
> 
> Give this a read - it explains layouts and how to use them better than  
> I can here.
> 
> [Ruby on Rails - Layouts](http://www.tutorialspoint.com/ruby-on-rails/rails-layouts.htm)

Oh, rails handles the automatic linking with the layout and views.

thanks.
