How to generate a layout for a controller


#1

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.


#2

On Wed, Dec 17, 2008 at 9:37 AM, Zhao Yi
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


#3

Franz S. wrote:

On Wed, Dec 17, 2008 at 9:37 AM, Zhao Yi
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.


#4

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


#5

Or you can set layout directly in your controller:

class MyController < ApplicationController
layout ‘some_other_layout’
end

Regards,
Bosko


#6

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.


#7

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.

http://www.tutorialspoint.com/ruby-on-rails/rails-layouts.htm


#8

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


#9

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.

http://www.tutorialspoint.com/ruby-on-rails/rails-layouts.htm

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

thanks.