Layouts and content_for

when i studied the tutorial about layouts & content_for from railscast.I need a clarification as per their tutorial.

url:#8 Layouts and content_for - RailsCasts

<!- projects/index.rhtml -> <% content_for :head do %> <= stylesheet_link_tag ‘projects’ %> < end %>

<!- layouts/application.rhtml -> <head> <title>Todo List</title> <%= stylesheet_link_tag ‘application’ %> <%= yield :head %> </head>

As per the tutorial they are calling the yiel:head & content_for :head has been written in index.html.erb.

My question is if i want to call another yield such as yield :footer and if i write content_for :footer in some other file like footer.html.erb. Then in my layout yield :footer is not taking.

Please give me some suggestions regarding this

Thanks in advance

You have to keep in mind how views are being called: - The browser makes a request to a URL - The Rails routing rules determine which controller/action the URL resolves to - The action is called - The corresponding view (assuming the action doesn't redirect to a new action) is being called - The controller (usually) determines which layout is to be used - The view is displayed together with the layout.

So: if you have an index.html.erb view being displayed, together with the layout application.html.erb, that's all that will be displayed. If your view doesn't define content_for for all the parts listed in you layout (with yield), those symbols remain undefined (nil) and don't display. Which is why your :footer part isn't displayed, there is nothing telling rails to display the footer.html.erb template.

If you want to render a footer partial, you can use <%= render :partial => shared/footer %> for example. That will actually include the shared/_footer.html.erb template in your page.

deegee wrote:

You have to keep in mind how views are being called: - The browser makes a request to a URL - The Rails routing rules determine which controller/action the URL resolves to - The action is called - The corresponding view (assuming the action doesn't redirect to a new action) is being called - The controller (usually) determines which layout is to be used - The view is displayed together with the layout.

So: if you have an index.html.erb view being displayed, together with the layout application.html.erb, that's all that will be displayed. If your view doesn't define content_for for all the parts listed in you layout (with yield), those symbols remain undefined (nil) and don't display. Which is why your :footer part isn't displayed, there is nothing telling rails to display the footer.html.erb template.

If you want to render a footer partial, you can use <%= render :partial => shared/footer %> for example. That will actually include the shared/_footer.html.erb template in your page.

On 17 sep, 11:17, Lost Warrior <rails-mailing-l...@andreas-s.net>

Thanks for ur reply. After made the post i just found the result.thanks for ur suggestion