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 %>
Related
I'm trying to render a partial on home page. Code is a follows:
<%= render :partial => 'spree/shared/slider' %>
The thing is I want it to render/show the partial only if I'm on the home page .To summaries for a particular page/pages only.
Can anyone help me with this? Thank you.
You can use a conditional for deciding whether the partial should be rendered:
<%= render :partial => 'spree/shared/slider' if #slider %>
The condition could be an instance variable (e.g. #slider) which can be set from the view (when not set, it remains nil by default):
<% #slider = true %>
Now the partial will only be rendered if #slider has been explicitly set to true in the view or in the controller.
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.
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 %>
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.
I have a view that has a block corresponding to a partial view (very simple).
<td WIDTH ="70%">
<%= render "partial_1" %>
</td>
Now, When the user clicks certain button, I make a Ajax call to my controller, and after my business logic is done, I want to return to the same view but rendering a different partial view.
def ajax_call
....
render :layout => "administracion"
end
I tried changing my <%= render "partial_1" %> for <%= yield %>
and in my controller: render :layout => "administracion", :partial => "mypartial"
but when I do this, only the partial is rendered, the other elements of my original view are lost.
What should I do?
I'm using rails 2.3.9
Thank you!
Look at the example here.
In a few words, you need to create a js.erb view and include your partial this way:
$('#your_container').html('<%=escape_javascript render("your_partial") %>');