How can I remove the layout application.html.erb from rendering on a particular page. It is now visible on all pages in my application.
You can override default rendering at the controller level.
class Admin::HomeController < Admin::BaseController
layout "admin"
You can also override rendering of layouts at a controller action level:
def show
render :layout => "layout_for_show_only"
end
And, if you are really desperate, you can override layouts in the view:
<%= render "print_view", :layout => "print" %>
See the excellent rails guide on the subject: layouts and rendering in Rails
ian.
You can simply add to the controller:
layout false, only: [:show, :edit]
which means, that application layout won't be rendered for the show and edit pages
Here is an answer.
You can set: format.js {render :layout=>false}
jQuery + Ajax + Haml. js.erb files not firing
Related
I am stuck at what I think is a very simple/common usecase in a Rails web application. I want to use "caches_action, layout:false" and display, from the layout, dynamic tags that will be set by the action (either from the view or the controller).
I could not find any standard rails way to do this as content_for does not work with caches_action, instance variables are not cached (?), and the metatags helper gems that I have tried (metamagic and meta-tags) do not support this usecase.
Is there any way to do this ?
Example
I am using caches_action, layout:false on a SandboxController#show method
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
def show
#meta_title = "Best page ever"
do_some_expensive_operation
end
end
The view
#app/views/sandbox/show.html.erb
We are in show action.
The layout
#app/views/layouts/application.html.erb
<title><%= #meta_title %></title>
Debug: <%= #meta_title %> <br/>
<%= yield %>
Thanks !
I found a way to make it work, it's not as pretty as I would like it to be but it helps using caches_action and setting HTML meta tags from the view.
Also, for the record, it seems that this was forgotten and buried deep down in the pipeline, as I did not find any recent mentions of this problem, only that caches_action and content_for together are not expected to work.
Solution: I simply add a before_action to set the meta tags by using as less computation as possible.
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
before_action :seo_show, only: :show
def seo_show
#meta_title = "Best page ever"
end
def show
do_some_expensive_operation
end
end
It's worth noting that it can be used in combination with metamagic gem too.
Layout:
#app/views/layouts/application.html.erb
<%= default_meta_tags && metamagic %>
<%= yield %>
And helper:
#app/helpers/application_helper.rb
module ApplicationHelper
def default_meta_tags
meta title: #meta_title || "Default meta-title of my website"
end
end
Hope this helps someone out there !
I'm using ActiveAdmin for a project. I have and I have a partial with a custom form. I want to be able to render this custom form when the user clicks in a button so this is what I did:
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
page_action :edit, :method => :get do
render partial:'custom_form', locals: { settings: welcome_message }
end
action_item do
link_to "Edit", admin_welcome_messages_edit_path, :method => :get
end
content do
render text: "HI"
end
end
This works, but the problem is that my form gets rendered without the layout and styles of ActiveAdmin, it just shows my custom_form as a clean html.
If I render my custom_form in the content do ... end it does work, but I need that to show something different.
Any help?? I don't know what else to try, I have reached the first 3 pages of google without success!!
kind of an old question but I was having the same problem and this solved it for me.
render partial: "name_of_your_partial", layout: "active_admin"
wasn't working for me either so I just changed my partial into a template.
render: "name_of_your_template", layout: "active_admin"
and now the activeadmin layout is being displayed.
I did not find an answer for this but I found a nice workaround that looks pretty good.
In the action_itemI have a link to admin_welcome_messages_path that is the main view and if I'm editing add a param there to show the form instead of the body.
Hope it helps somebody!
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
action_item do
link_to "Edit", admin_welcome_messages_path(edit: true), :method => :get
end
content do
if params["edit"] == "true"
render partial:'form', locals: { settings: welcome_message }
else
render partial:'body'
end
end
end
Add layout option to the render call.
render partial:'custom_form', layout: 'active_admin', locals: { settings: welcome_message }
Determine which layout to use.
If we're rendering a standard Active Admin action, we want layout(false)
because these actions are subclasses of the Base page (which implements
all the required layout code)
If we're rendering a custom action, we'll use the active_admin layout so
that users can render any template inside Active Admin.
https://github.com/activeadmin/activeadmin/commit/ce0927661c5991cc857306363c402ef9e283cc42
# this would automatically render the custom_edit.html.erb view
page_action :custom_edit do
end
action_item do
link_to "Custom Edit", admin_welcome_messages_custom_edit_path
end
I realise that this an old question but the following worked for me for my Ruby on Rails application:
Inside your your_custom_page.rb file do the following
content do
render 'partial_name'
end
and place the _partial_name.erb in view/admin/your_custom_page/
Your partial should now render on that active admin page.
I have a page which displays my testimonials in the my_testimonials action of the testimonials controller.
The correct layout for this action is layouts/frame but I must have changed something because now it is rendering layouts/application
Here is the layout being specified in the controller:
layout "layouts/frame", only: [:my_testimonials]
layout "layouts/shares", only: [:new]
Here is the output in the console:
Started GET "/my_testimonials/c4ca4238a0b923820dcc509a6f75849b" for 105.228.65.202 at 2013-09-27 07:40:49 +0000
2013-09-27T07:40:49.470211+00:00 app[web.2]: Processing by TestimonialsController#my_testimonials as HTML
2013-09-27T07:40:49.686047+00:00 app[web.2]: Rendered testimonials/_testimonial.html.erb (2.6ms)
2013-09-27T07:40:49.686200+00:00 app[web.2]: Rendered testimonials/my_testimonials.html.erb within layouts/application (79.2ms)
The testimonials are being displayed but with the incorrect layout. As I said it was working yesterday so I must have changed some small detail or the order of something.
I'm inclined to think that a layout() call overrides previous calls. In this case, that the last layout() call you did for :new overrode the one for :my_testimonials.
You can also choose the layout at runtime:
layout :set_layout
protected
def set_layout
case action_name.to_sym
when :my_testimonials
'frame'
when :new
'shares'
else
'application'
end
end
Or specify your layout per action:
def my_testimonials
respond_to do |format|
format.html { render :layout => 'frame' }
end
end
Update:
The code suggests they do override previous calls. So calling layout "layouts/shares", only: [:new] is essentially saying to use "shares" for :new and "application" otherwise.
Rails looks for the layout in the 'app/layouts/' folder. See Section 2.2.2
So, when you specify 'layouts/frame', rails looks for 'app/layouts/layouts/frame', which it obviously cannot find.
Change your layout directives to the following:
layout "frame", only: [:my_testimonials]
layout "shares", only: [:new]
change
layout "layouts/frame"
to
layout 'frame'
I think this should work.
I have an application.html.erb file which sets out layout for every pages in my app (header, footer etc.) like a typical Rails app.
However, I would like to have a landing page which I don't want to use this file. How should I bypass application.html.erb?
Thank you.
Use
render :layout => false
or
render :layout => 'whatever'
in your action. If you are using a separate LandingController you simply can create a app/views/layouts/landing.html.erb which will be picked up or you can override the layout via
class LandingController < ApplicationController
layout 'whatever'
...
end
You can set a layout in your render function:
render {other arguments}, :layout => :homepage
You can also set that option to false to not use any layout at all.
You can do something similar if you want an entire controller to use a custom layout:
class MyController < ApplicationController
layout :homepage
#...
end
Hope that helps!
In the controller that renders the view, change the render to:
render :layout => false
You can read more about options to render and how to work with layouts at the Rails guide to render and layouts.
When try I following code in a controller, the view renders without using the layout
def xyz
render :partial => 'platinum_home', :layout => 'platinum_layout'
end
But If I do the following inside the partial
<% render(:layout => "platinum_layout") do %>
blah blah blah
<% end %>
It works just fine, is the first example not possible using rails?
In your controller at the top add the following:
class SomeController < ApplicationController
layout "platinum_layout", :only => :xyz
Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
So to use current layout for your just used.
def xyz
render :partial => 'platinum_home', :layout => true
end