Forum: Ruby on Rails How to generate a layout for a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Zhao Y. (Guest)
on 2008-12-17 10:37
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.
Franz S. (Guest)
on 2008-12-17 10:43
(Received via mailing list)
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
Zhao Y. (Guest)
on 2008-12-17 10:50
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.
Hafeez S. (Guest)
on 2008-12-17 10:56
(Received via mailing list)
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
John Y. (Guest)
on 2008-12-17 17:18
(Received via mailing list)
> 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.
Bosko (Guest)
on 2008-12-17 18:59
(Received via mailing list)
Or you can set layout directly in your controller:

class MyController < ApplicationController
  layout 'some_other_layout'
end

Regards,
Bosko
Zhao Y. (Guest)
on 2008-12-18 02:52
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
John Y. (Guest)
on 2008-12-18 03:47
(Received via mailing list)
> 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-...
Zhao Y. (Guest)
on 2008-12-18 03:49
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-...

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

thanks.
This topic is locked and can not be replied to.