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?
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
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
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
Related
Most of my view files have the same layouts, hence it was reasonable to define layouts/application.html.haml (and other header, footer files).
But now I need to create a page that does NOT have any of those styling.
In fact, I just want a plain page with a header.
How do I do that?
I think you're after. In your controller, assuming your action is called myaction
def myaction
# do here whatever you need to do and then
render :layout => false
end
See options for render in Rails Guide: layouts and redendering
You can specify the layout in the controller like so:
class ThingsController < ApplicationController
layout "some_layout"
# rest of the controller
end
This would look for app/views/layouts/some_layout.html.erb
You can also use a method to choose the layout:
class ThingsController < ApplicationController
layout :choose_layout
def choose_layout
current_user.cat? ? "cat" : "dog"
end
# rest of the controller
end
I want a particular page in the application to have different layout, while all others will have pretty much the same layout. However, the application.html.erb file is rendered for all the pages in application. How do I not use the application.html.erb file for a particular page?
Rails version : 3.2.1
You can make another layout and specify and in your action you can simply use that as follow
class ReportsController < ApplicationController
before_filter :authenticate_affiliate_user!
def daily_breakdown
render :layout => 'admin_layout'
end
end
to you different layout in all of the action you can do as follow
class ReportsController < ApplicationController
layout 'reporting_affiliate'
before_filter :authenticate_affiliate_user!
# your code here
end
Hi you can use layout option or specify particular layout in action
Class SomeControlle < ...
layout :admin_layout,:only=>[:some_action]
def some_action
#or
render :layout=>'admin_layout'
end
end
Use render :layout => 'special_layout' in your controller where you render.
I want to all my views from different paths to use the 'layouts/application.html.erb', except a view that has a specific structure. Is that possible without forcing to create a layout for this view and each one for others?
The 'index.html.erb' couldn`t use the layout 'application.html.erb' in my case.
You can override the default layout by passing an explicit layout name in the render call.
class FoosController < ApplicationController
def index
# call below uses layouts\new_layout.html.erb as the layout
render :layout => 'new_layout'
# if you want to render without a layout
# render :layout => false
end
end
This is easy. If you want to use a different layout for the entire controller, just put the following at the top of that controller:
class ItemsController < ApplicationController
layout "inventory"
#...
end
I have an application where my homepage is significantly different from other pages. SO can you suggest how I should use my layout?
You can change the layout for all actions in a controller with the following code:
class ThingsController < ApplicationController
layout "my_layout"
...
end
You can change the layout for a specific action using the following code:
def ThingsController < ApplicationController
def action
...
# to render "app/views/things/action"
render :layout => "my_layout"
# or to render a specific view
render "pages/something", :layout => "my_layout"
end
end
If I got it right you are asking about how to manage multiple layouts for your project. What you could do is simply adding your layouts inside the layout directory and by referencing the one you want to use by adding this line to the controller:
layout "name_of_your_layout"
I need message to have a different layout in project, is it possible in rails to do something like this?
Class Messages::New < #project? ProjectLayout : NormalLayout
end #i treid this, don't work, since #project has not been initiated.
thanks
this may help you
class MessagesController < ApplicationController
layout :get_layout
def get_layout
#project? ? 'ProjectLayout' : 'NormalLayout'
end
end
Also, since the question is unclear, you can also set layout for only one action with the render option.
render :action => 'new', :layout => 'layoutname'
You can only apply layouts at the Controller level:
class MessagesController < ApplicationController
layout :project
end
Layout method documentation has an example on how to do conditional layouts
You can only apply the rails layouts at controller level and individual action levels.
Unique layout in for each controller
class MessagesController < ApplicationController
layout "admin"
def index
# logic
end
end
** The above line layout "admin" will load the admin layout each time the message controller gets invoked. For that, you must have a layout created in your layouts/admin.html.rb file.**
Dynamic layout for each controller
class MessagesController < ApplicationController
layout :dynamic_layout
def index
# logic
end
protected
def dynamic_layout
if current_user.admin?
"admin" # Show admin layout
else
"other_layout" # Show other_layout
end
end
end
# Individual Action Level Layouts
If you want to display different layouts for each action you can do that.
class MessagesController < ApplicationController
layout :dynamic_layout
def index
# logic
render :action => 'index', :layout => 'index_layout'
end
def show
# logic
render :action => 'show', :layout => 'show_layout'
end
end
Decide the layout in the controller rather then the model. Your ProjectsController can use it's own ProjectLayout and MessagesController can then use the normal layout if you wish.
My two cents in ApplicationController:
before_action :layout_by_action
##actions = %w(new edit create update index)
def layout_by_action
if ##actions.include? params[:action]
self.class.layout 'admin'
else
self.class.layout 'application'
end
end
You can use a Proc:
layout -> {
if something?
'my-layout'
else
'my-other-layout'
end
}