In my controller I have:
render :layout => 'mobile'
In my view I want to be able to get the name of the layout, in this case "mobile".
I don't need the controller or action name...just the name of the layout.
I'm using Rails 3.0.6.
I was late, but try this:
controller.send(:_layout).virtual_path.name
Tested in Rails 3.2
why you dont make an variable in your controller:
#layout = 'mobile'
render :layout => #layout
then you can access #layout in your view
Related
This may be a silly question, but I'm curious to know how it works. Here is the scenario:
Controller
def some_method
#abc = true
render :template => "show", :layout => "lightbox"
end
Now, when I try to access the instance variable #abc inside layout, it comes out to be nil. But, for following scenario:
Controller
def some_method
render :template => "show", :layout => "lightbox"
end
View (show.html.haml)
- #abc = true
The variable is accessible inside layout.
My question is:
Why is it different that I define an instance variable inside view, it's accessible in layout, but if the same variable is defined in controller instead of view, it's not accessible and come as nil?
In the first case: When you use render in the controller action, it does not inherit the controller action scope automatically. You would have to pass the variable to the render call. Something like:
render :template => "show", :layout => "lightbox", :locals => {abc: #abc}
In the view template you would be able to access the variable abc (without the #), containing the controller #abc variable contents
In the second case: when processing the template "show", rails is creating the #abc variable inside the view scope. So you can access it.
The answers provided in this other question can help you understand what is happening. And the Rails API docs are also a good reference for you
In my Rails 3 app, my 'show' view renders a submitted form. I'd like to create an 'info' view that displays different data from the same submitted form. It's basically an alternate show view.
I put this into my controller, but it doesn't work:
def show
#form = Form.find(params[:id])
end
def info
#form = Form.find(params[:id])
render :layout => 'info'
end
All it does is display the 'show' view. How do I correct this?
The problem must lie elsewhere, because those two actions look fine. In this example, your show action should use your show view, and your info action should use your info view (albeit with a layout also called "info" - you might want to consider renaming that so you don't get confused between your layout and your view).
Are you sure your routes are set up properly? Do you have an 'info' route set up for your Form model? i.e.
routes.rb:
resources :forms do
get :info
end
And are you navigating to the right URL? (in this case, http://yourhost/forms/123/info)?
You can call
def info
#form = Form.find(params[:id])
render :action => 'show'
end
and it will use the #form instance variable defined in the info method, make sure you have the proper routes setup
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.
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
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