Nick
Nick

Reputation: 3140

How to make a specific view not inherit from application.html.erb?

I have one specific view that I don't want to "inherit" the code from layouts/application.html.erb. How should it do this?

One way to do this is to include in application.html.erb:

<% except current_page?(page_path) %>
  content
<% end %>

But it feels to me more like something you should specify in the specific view. What would be the convention for this matter?

Upvotes: 1

Views: 990

Answers (3)

Akshay Borade
Akshay Borade

Reputation: 2472

For selected actions you can try like this ......

class MyContorller < ApplicationController
  layout 'other_layout_name', only: [:method_1, :method_2, :method_4] 

  #action methods here
end

Upvotes: 1

Ioannis Tziligkakis
Ioannis Tziligkakis

Reputation: 711

If you don't want a layout to apply to a specific action you can disable layout rendering at the controller level. For example if you want MyController#some_action view to not be rendered within the layout, you can do this:

class MyContorller < ApplicationController
  def some_action
    # Your logic here
    render layout: false
  end
end

This way your view (app/views/my_controller/some_action.html.erb) doesn't inherit from the application.html.erb and is rendered containing only what's inside the view file. For more information on rendering you can take a look here

Upvotes: 2

Mihail Petkov
Mihail Petkov

Reputation: 1545

For one action, you can try:

class TestController < ApplicationController
  def method_name
    render :layout => 'another_layout'
  end
end

For all actions in controller, you can do:

class TestController < ApplicationController
  layout 'another_layout'

  #action methods here
end

Upvotes: 4

Related Questions