Forum: Ruby on Rails What is the matter with content_for_layout?

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.
A8397e5af481271e9f83f591dc015116?d=identicon&s=25 Tauraus (Guest)
on 2006-03-13 12:11
Here is my layout file:
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <title>My Demands</title>
  <%= stylesheet_link_tag 'ajax_scaffold', :media => 'all' %>
  <%= javascript_include_tag 'prototype', 'effects', 'rico_corner',
'ajax_scaffold' %>
</head>
<body>
<div id="container">
<%= @content_for_layout %>
</div>
</body>
</html>


After I start up the Webrick server,the html source will be as follows:
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <title>My Demands</title>
  <link href="/stylesheets/ajax_scaffold.css" media="all"
rel="Stylesheet" type="text/css" />
  <script src="/javascripts/prototype.js"
type="text/javascript"></script>
<script src="/javascripts/effects.js" type="text/javascript"></script>
<script src="/javascripts/rico_corner.js"
type="text/javascript"></script>
<script src="/javascripts/ajax_scaffold.js"
type="text/javascript"></script>

</head>
<body>
<div id="container">
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <title>My Demands</title>
  <link href="/stylesheets/ajax_scaffold.css" media="all"
rel="Stylesheet" type="text/css" />
  <script src="/javascripts/prototype.js"
type="text/javascript"></script>
<script src="/javascripts/effects.js" type="text/javascript"></script>
<script src="/javascripts/rico_corner.js"
type="text/javascript"></script>

<script src="/javascripts/ajax_scaffold.js"
type="text/javascript"></script>
</head>
<body>
<div id="container">
...



It seems that @content_for_layout will contain all the content include
the part before <body>,how can it happen? very thanks!
C5b6840775639751136a320d3cb47b12?d=identicon&s=25 Cheltis (Guest)
on 2006-03-13 12:20
Most likely you have that content in your .rhtml files too

Tauraus wrote:
> It seems that @content_for_layout will contain all the content include
> the part before <body>,how can it happen? very thanks!
A8397e5af481271e9f83f591dc015116?d=identicon&s=25 Tauraus (Guest)
on 2006-03-13 12:28
Cheltis wrote:
> Most likely you have that content in your .rhtml files too
>
> Tauraus wrote:
>> It seems that @content_for_layout will contain all the content include
>> the part before <body>,how can it happen? very thanks!

I had examined other rhtml file for times,but it does not exist at all..
A8397e5af481271e9f83f591dc015116?d=identicon&s=25 Tauraus (Guest)
on 2006-03-13 13:34
Tauraus wrote:
> Cheltis wrote:
>> Most likely you have that content in your .rhtml files too
>>
>> Tauraus wrote:
>>> It seems that @content_for_layout will contain all the content include
>>> the part before <body>,how can it happen? very thanks!
>
> I had examined other rhtml file for times,but it does not exist at all..

I got the reason,but I still do not know how to resolve this problem.
It is because of my using pagination.
Here is the demands_controller:

def index
end

def list
  @pages = Paginator.new self, Demand.count, 10, @params[:page]
  @demands = Demand.find(:all, :order => 'reply_times DESC', :limit =>
10, :offset => @pages.current.offset)
end

Here is the index.rhtml:
<%= render_component :controller => 'demands', :action => 'list' %>
it is generated by the Ajaxscaffold.

if the url refers to : http://127.0.0.1:3000/demands or
http://127.0.0.1:3000/index
then the above error will happen
if the url refers to : http://122.0.0.1:3000/demands/list
then everything is ok

Can anybody tell me the reason? very thanksful!
C5b6840775639751136a320d3cb47b12?d=identicon&s=25 Cheltis (Guest)
on 2006-03-13 13:59
OK, now it looks clear

layout is dublicated because it applied twice

you should put something like this in your controller
layout "your_layout", :except => 'list'

however, correct way of doing this would be removing index.rhtml and
changing index action to

def index
  list
  render :action => 'list'
end

Tauraus wrote:
> I got the reason,but I still do not know how to resolve this problem.
> It is because of my using pagination.
> Here is the demands_controller:
>
> def index
> end
>
> def list
>   @pages = Paginator.new self, Demand.count, 10, @params[:page]
>   @demands = Demand.find(:all, :order => 'reply_times DESC', :limit =>
> 10, :offset => @pages.current.offset)
> end
>
> Here is the index.rhtml:
> <%= render_component :controller => 'demands', :action => 'list' %>
> it is generated by the Ajaxscaffold.
>
> if the url refers to : http://127.0.0.1:3000/demands or
> http://127.0.0.1:3000/index
> then the above error will happen
> if the url refers to : http://122.0.0.1:3000/demands/list
> then everything is ok
>
> Can anybody tell me the reason? very thanksful!
A8397e5af481271e9f83f591dc015116?d=identicon&s=25 Tauraus (Guest)
on 2006-03-13 14:05
Cool,aha! Thanks a lot,Cheltis :-)

Cheltis wrote:
> OK, now it looks clear
>
> layout is dublicated because it applied twice
>
> you should put something like this in your controller
> layout "your_layout", :except => 'list'
>
> however, correct way of doing this would be removing index.rhtml and
> changing index action to
>
> def index
>   list
>   render :action => 'list'
> end
This topic is locked and can not be replied to.