Render parial on certain action - ruby-on-rails

Probably a newbie question, but when my user visits job#index I want to show a partial that is stored in my views/layouts called _subnav.html.erb.
This only needs to render when the user is at jobs#index.
Can someone help me with this but also understand how it works.

I'd suggest you use content blocks.
Within your layout file, such as layout/application.haml, place this code where you want the subnav to be rendered:
= content_for :subnav
..and within your view file (jobs/index.haml)
- content_for :subnav
= render partial: 'layouts/subnav'
Couple of alternatives:
Check controller#action in layout file and only show for jobs#index
Add a simple #show_sub_nav instance variable and set to true within your controller. In your layout file: = render partial: 'layouts/subnav' if #show_sub_nav
You can see how to achieve (2) with my answer here.

Related

Rails 5: Rendering a partial to the application layout

Within views/layouts/application.html I have the line <%= yield %>. This line is ultimately replaced by views directly corresponding to controller actions without any problems.
In one of my controllers, I am trying to render a partial instead of the default behaviour:
def show
#service_groups = ServiceGroup.where(deleted_at: nil)
render partial: 'table', locals: {rows: #service_groups, headers: service_group_headers}
end
I'm using a partial in this way so that I can use the same basic table structure for various different database tables (across different controllers).
This render partial code doesn't seem to work with the <%= yield %> line in the application layout. The partial code is just rendered on its own without the surrounding layout.
Why is this?
How do I rectify the problem?
Please let me know if I should be handling this a different way.
Thanks.
(migrating from comments)
What if you created a show.html.erb for that controller and placed render :partial there? It should work.
Explanation: Partials are to be used from "big" views. So, they are not wrapped in the layout on purpose!

Render :layout is searching for partial instead of layout

I understand this is a bad idea, but from what I've seen in ApplicationControllers, using:
render :layout => "something" ...
Should render using a layout located at views/layouts/something.html.erb
However, when I am making this call from inside of a view, it errors out with:
Missing partial my_controller_name/something with ...
Searched in:
* "{path here}/app/views"
Which seems to me its looking for a partial, instead of a layout as I specified. Does anyone know what is going on with that?
A sufficient example small enough to reproduce it:
<%= render :layout => 'something' do %>
<div>Hello</div>
<% end %>
This is all under Rails vs 4.0.2
render works differently in controllers than it does in views. In controllers, it's primarily for rendering action templates, while in views, it's primarily for rendering partial templates. When you want to render a specific layout for an action, you have a few options, but all of them are in the controller.
If you want every action in a particular controller to use that layout, you can either specify layout 'something' in that controller (usually near the top) or for a ApplesController, you can create a new layout in app/views/layouts/apples.html.erb and this will automatically be used as the default layout for the ApplesController.
If you want just a single action in a controller to use that layout, you can use your render layout: 'something' inside of a controller action, where the action to render is implied to be the current action.
Links from the Rails docs:
Action Rendering
Partial Rendering
Nested Layouts

rails rendering partial with collection / other class is in charge?

Somewhat new to rails, longtime programmer. I've got a question about views, controllers and partials really - wondering if I have this setup well.
I've got a pages controller, and on the index page (really the pages index method) I've got a partial in layouts called featured (ie app/views/layouts/_featured.html.erb) -- I've also got a Featured class. I would like basically the index of the featured class to be drawn here. But of course it's not working. SO the question is:
In the page itself I've got the <%= render 'features/index' %> which I'm beginning to think is the wrong way to go..
Do I axe this partial method and just call <%= render 'features/index' %> and let everything progress natively or
What would be the proper way of routing the featured collection to the partial? Since the controller is actually Pages it seems like I'm fighting against the tide.
<%= render 'features/index' %>
Doing this is wrong given your description. This will try to render a partial from app/views/features/_index.html.erb which you haven't mentioned.
To render the partial at app/views/layouts/_featured.html.erb you would do (perhaps a bit more verbose that is necessary)
<%= render partial: "layouts/featured" %>
The best suggestion I can offer is to pass a collection to this partial
<%= render partial: "layouts/featured", locals: { features: #features } %>
Since it seems your intention is for this partial to appear as a piece of a layout I will assume you wish for this partial to appear on multiple pages. This means on multiple actions you will need to have assigned the set of Feature instances this #features instance variable. One way to do this is a before_action.
before_action :setup_features
# ...
private
def setup_features
#features = Feature.all
end
A good place to start learning more about filters is in the Rails Guide
The partial at "app/view/layouts/_featured.html.erb" can only be rendered with
render 'featured'
and not 'featured/index'
render 'featured/index' will render "app/views/layouts/featured/_index.html.erb
Since the pages controller is really rendering the main index page in it's def index, all I had to do was #features = Feature.all and the variable is available for the partial pulled into the index page.
I need to get used to how simple rails is coming from other languages / frameworks.

application.html.erb not loading

well, the title by itself describes: the application.html.erb, even if I put this in the controller: layout "application"
well, here is the code of the application.html.erb, application_controller and public_controller:
https://gist.github.com/897408
All of your methods either use render :text or render :partial, neither of which make use of a layout. You need to create a view for each method. For instance, a view for contact in your PublicController would go in views/public/contact.html.erb. If you have a view at that location for contact, you don't need to put a render call at all.
Take a look at this link for some more basics on layouts and rendering.

render a view within a view

How do I render a view within another view... in rails... is it possible?
#hotdog_controller
def show
# by default renders template hotdog/show.html.erb
end
#hotdog/show.html.erb
<%= render :template => 'ketchup' -%>
Use view partials
See section 3.4 http://guides.rubyonrails.org/layouts_and_rendering.html
A complete view inside another a view? Normally the way you would handle something like this in rails would be with partials.
Create a set of shared partials, and then just render them in each of the views.
I do the same thing a few times, but I load the sub views in through AJAX. And that AJAX is instantiated within the partial.

Resources