Is is possible to render a partial as a layout?
Currently, I have a partial _show.html.erb under app/views/users. In some other controllers, I tried to include layout 'users/show' to use that partial as a layout.
Yet, by default, Rails seems to be looking for layouts in layouts/ directory. And I get such error as:
Template is missing
Missing template layouts/users/show ....
Any suggestions?
I think that if you just want to include a partial in another view you should user render:
<%= render partial: 'users/show' %>
Perhaps in your other controller's action you have:
#user = #some_object.user
You'll need to pass in the #user to your partial. You might want to refer to a local variable, user, in your users/show partial and pass in the instance variable:
<%= render partial: 'users/show', locals: {user: #user} %>
You can also use the partial and layout options to together so that a partial will render inside a specified layout. For example, you could specify that your
<%= render partial: "contact_info", layout: "users/show" %>
This would tell the contact_info partial to render inside a layout found in app/layouts/users/show.html.erb.
Related
I've tried to make sense of layouts but got lost..Also googled & looked at other similar questions on SO but none of them helped.
Say I have MVC's (scaffold'ed) for A and B, creating their ..views/A/index.html.erb and ..views/B/show.html.erb among the rest.
A's index method sets a #a_collection.
Within B's show view I want to:
<p>..stuff for B..</p>
<%= render A's index %>
<p>..some more B-stuff</p>
How can I render A's index in that place in B's show?
You don't typically render a view inside of another view. You use partials to share code across views. For example:
# app/views/products/_product.html.erb
# this is the code you want to reuse
<p>Product Name: <%= product.name %></p>
# app/views/products/index.html.erb
<%= render #products %>
# app/views/stores/show.html.erb
<h1><%= #store.name %></h1>
<h2>Our Products</h2>
<%= render #store.products %>
<%= render #products %> is shorthand for <%= render partial: "product", collection: #products %>.
This is just the implicit rendering - in many cases you'll want to add more partials and render them explicitly. Like for example the _form.html.erb partial that you'll find in the scaffolds thats used to share a form between the create and edit views.
Think of partials like the view equivilent to a function - ideally they should take some input in the form of locals and result in a chunk of HTML.
I am trying to render a partial using an if statement and this renders alternate content. If I wanted to do this and render the content itself using a different layout (one without a header and footer for example), is this possible?
<%= render partial: "partialname", layout: "layouts/blank" %>
I've looked around and tried doing this in the view, but get an error which states
'Missing partial layouts/_blank'
Am I missing something? The layout isn't a partial, so unsure why it's using an underscore.
Am I better off doing this in the controller?
Try
<%= render partial: "partialname", :layout => false %>
Im trying to bring a layout in my html page by
render layout 'flatty'
thing is this loads the whole flatty layout. In flatty.html.erb it renders _header,_footer and also _sidebar.html.erb
I dont want to load _sidebar.html.erb in this particular page.
So how should i render this?
thing is this loads the whole flatty layout. In flatty.html.erb it renders _header,_footer and also _sidebar.html.erb I dont want to load _sidebar.html.erb in this particular page
Why do you want to use same layout if you have so many changes? Why not make a partial which you could render in both cases. Make a new partial, lets say _common.html.erb, render it in your flatty layout and view of the action in which you want to use it.
#flatty.html.erb
<%= render "common" %>
<%= render "sidebar" %>
#some_action_name.html.erb
<%= render "common" %>
If you still want to use same layout in both cases then you ca use rails 4 controller_name and action_name helpers in your layout and selectively render sidebar and other partials in your layout:
#flatty.html.erb
<%= if controller_name == "some_controller_name" && action_name == "some_action_name"
<%= render "sidebar" %>
<% end %>
Maybe in your controller action, you can have a flag indicating the sidebar should not be rendered. Then, in your flatty.html.erb file, check for the flag variable before you render the _sidebar.html.erb.
For example, if you have a controller action called flatty, add an instance variable, #disable_sidebar, to act as your flag.
def flatty
#disable_sidebar = true
# Your other code
render layout: 'flatty'
end
Then, in your flatty.html.erb, add a conditional before your render for your sidebar (note the ! negation in the if statement:
<% if !#disable_sidebar %>
<%= render "layouts/sidebar" %>
<% end %>
Alternatively, in your flatty.html.erb you can also check for the controller and action values in your params hash, and then don't render your sidebar if it matches that controller's action:
<% if params[:controller]!="YOUR_CONTROLLER" and !params[:action].eql? "flatty" %>
<%= render "layouts/sidebar" %>
<% end %>
Quick rails question. When I do this:
<%= render #users %>
Rails will search for
_user.html.erb
Is there option to render different file?
EDIT
I do not made custom partial - I use params[:action] to determine what kind of html I will render for partial.
Yes, there is.
<%= render partial: 'path/to/partial', collection: #users %>
Read more about this on Rails Guides.
Rails looks for a method on the model called to_partial_path to determine which partial to render. So you can override this method to define your own partial within the model logic. See #4 in this post: http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/
I have a main layout (application) and two 'sub' layouts (dashboard and admin). In my dashboard and admin controllers respectively I have a before_filter which render templates: the template I want (either dashboard or admin).
In my dashboard and admin layouts, I am doing something along the lines of:
<% content_for :top_menu do %>
<%= render partial: "layouts/menu/top", locals: {section: 'admin'} %>
<%= render partial: "layouts/menu/sub", locals: {section: 'admin'} %>
<% end %>
So this is including the top partials with a section local which shows the correct options I want.
In my application layout, I have the following:
SOME HTML HERE
<%= yield :top_menu %>
SOME HTML HERE
<%= yield %>
SOME HTML HERE
The problem is that the content from my views isn't being displayed, I'd expect it to be displayed where the 'yield' is in my application layout.
I have read: http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts - but following that just displays the menus twice and still doesn't display my content.
I think I am failing to understand something here, help would be appreciated.
In short, I want top menus and it's in the controller that I want to specify which menu is to be used. I'm sure there is a better solution to this that I am missing also.
I have fixed this by doing:
layout 'menu/admin'
In my controller, and adding:
<%= render template: "layouts/application" %>
To my layouts.