roger
roger

Reputation: 9893

can view not extend "application/application.html.erb" in ruby on rails?

As we all know, in ruby on rails, all views extends from application/application.html.erb, most of the time this is great, such as the application.html.erb as follow:

<html>
<head></head>

<body>

<%= render 'layouts/header' %>
<%= yield %>

</body>
</html>

I do not need the write the same code in every view again, but sometimes, just on view is special, this view is different from the view, such as I do not want to add <%= render 'layouts/header' %> in this view.

Maybe a parameter will just help me in this situation, but I want to know if any view is able to not extend from application/application.html.erb?

Upvotes: 1

Views: 532

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

Views don't 'extend from' application.html.erb, they use it as a default layout. You can change it, of course, using layout method in controller (or layout option in render method), like this:

# this changes the default layout in every views of `AdminController` (and all other controllers that inherit from `AdminController`):
class AdminController < ApplicationController
  layout :admin
  # ...
end
# this changes the layout of specific action:
class SomethingController < ApplicationController
  # ...
  def some_action
    # ...
    render layout: :some_layout
  end
end

Here's the reference: http://guides.rubyonrails.org/layouts_and_rendering.html

Upvotes: 4

Related Questions