How to Use Views and Layouts with Ruby and Erb (Not Rails)

How can I use views and layouts with Ruby and ERB (not Rails)?

Thanks for all the answers!

I solved it finally by doing this, I hope someone else also can find this code useful:

def render_with_layout(template_path, context = self)
template = File.read(template_path)
render_layout do
ERB.new(template).result(context.get_binding)
end
end

def render_layout
layout = File.read('views/layouts/app.html.erb')
ERB.new(layout).result(binding)
end

And I call it like this:

def index
@books = Book.all
body = render_with_layout('views/books/index.html.erb')
[200, {}, [body]]
end

Then it will render my view, with the hardcoded (so far) layout..

Ruby on Rails view not pulling in application.html.erb layout

Several things (should be a comment, but I'll write it here for clarity):

  1. Check you have app/views/layouts/application.html.erb
  2. Check you have app/controllers/application_controller.rb inheriting from ActionController::Base

When you mention the use of <%= render "form" %>, what do you mean? Where is this rendered? If it's rendered from an action, it will be shown on your application layout automatically

Can you post more code for us, specifically posts_controller, application_controller, routes and application layout file?

rails 3, how add a view that does not use same layout as rest of app?

By default, layouts/application.html.haml (.erb if you are not using haml).

In fact, layout file could be set per controller or per action, instead of per view, per view folder.

There are few cases:

To change the default layout file for all controller (ie. use another.html.haml instead of application.html.haml)

class ApplicationController < ActionController::Base
layout "another"

# another way
layout :another_by_method
private
def another_by_method
if current_user.nil?
"normal_layout"
else
"member_layout"
end
end
end

To change all actions in a certain controller to use another layout file

class SessionsController < ActionController::Base
layout "sessions_layout"
# similar to the case in application controller, you could assign a method instead
end

To change an action to use other layout file

def my_action
if current_user.nil?
render :layout => "normal_layout"
else
render :action => "could_like_this", :layout => "member_layout"
end
end

I thought views/layouts/application.html.erb was meant to apply to all layouts?

Both your layouts, application.html.erb and posts.html.erb should render the main bar as a partial:

<%= render :partial => 'layouts/main_bar' %>

All layouts contain the <html> element that wraps your page; you don't have one layout wrapping around the other.

Rails aways renders the application.html.erb instead of wanted views

By default, a controller action in Rails renders the view template for your action, wrapped up into a layout (which is application/layout)

ActionView::TemplateHandlers manages the lookup for the extension (.html.erb, .html.haml, .json.erb, etc ...)

so, in an action called index, you will get this implicit call unless you call render yourself :

def edit
# your code
render action :'edit', layout: 'application/layout' # implicitly called
end

Rails will then start processing your layout and put the content of your edit template in place of any yield within your layout. Thus, a typical layout will look like this :

<!doctype html>
<head>
</head>
<body>
<!-- layout content before view -->

<%= yield %>

<!-- layout content after view -->
</body>

Chicken/egg: does rails call a layout for a view, or a view from a layout?

Views are called from the controller. So for instance posts_controller index action will look in the views folder with the same name as the controller then look for the template with the same name as the action... app/views/posts/index.html.erb. This you already know.

Similarly it will by default look in the app/views/layouts folder for a layout with the same name as the controller... in this case it will look for app/views/layouts/posts.html.erb. It if does not find a file by this name it will default to app/views/layouts/application.html.erb as the default layout template.

The Rails magic will put the two together, with the index template going into the <%= yield %> area.

How can I render a partial view from within a layout file?

I believe your path is wrong in this case. Change the code to

<%= render 'layouts/footer' %>


Related Topics



Leave a reply



Submit