Comments on How does a view know it should show the category bar?
Parent
How does a view know it should show the category bar? Question
Pages that are "in" a category show the category bar at the top, like this:
An individual suggested edit should logically be "in" a category too, but isn't shown that way:
I'm trying to figure out how that bar gets added to pages, so I can fix this, but I'm having trouble understanding the view code.
It's apparently not in the individual page views themselves. For example, the view for that first screenshot, app/views/suggested_edit/category_index.html.erb
, begins with the <h1>
for the title. I looked at a couple more, like app/views/tags/show.html.erb
and app/views/posts/show.html.erb
, and found the same thing (modulo some HTML header meta stuff that I don't understand but seems unrelated).
I see that app/views/layouts
exists, so I looked around in there. _header.html.erb
contains the actual category bar, with the category name and links for posts, tags, and edits. application.html.erb
renders it. Taking a closer look at _header.html.erb
, I see some checks around this code block:
<% if expandable? %>
<div class="container category-header--container">
<!-- ... -->
<div class="category-header--name">
<%= current_cat.name %>
</div>
<div class="category-header--nav">
<!-- ... link to Posts, link to Tags -->
<%= link_to suggested_edits_queue_path(current_cat),
class: "category-header--nav-item #{active?(current_cat) && controller_name == 'suggested_edit' ? 'is-active' : ''}" do %>
Edits
<% if pending_suggestions? && check_your_privilege('edit_posts') %>
<span class="badge is-status" title="Suggested edits pending"></span>
<% end %>
<% end %>
<!-- ... -->
This expandable
check seems to be the key to getting this bar to show up, but that's where I got stuck. How do other pages (posts, category, tags, the actual list of edits) get set to "expandable"? It's not being set in their views. I looked around in the controllers and didn't find it there, and it's not a database column (didn't expect it to be, but checked anyway). Or is expandable
a red herring, and I need to be looking for something else in all these views?
--
An answer helpfully points out where expandable?
is defined (and shows the code), but now I'm even more confused. That function returns true if post
is defined and its category is not nil
. The view for an individual suggested edit already contains:
<% post = @edit.post %>
To which I added (this should be redundant):
<% category = @edit.post.category %>
The post is properly formed and has a category. (You can see this if you click through to the post from the suggested edit.) So why isn't the category header showing up, either already or with my added definition of category
?
Post
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Monica Cellio |
Thread: Works for me Also need to modify the |
Mar 4, 2025 at 01:06 |
Hidden helper functions
There are additional helper functions defined that can be used from the view files. These can be found in a separate helpers
directory. In this particular case, the expandable?
function is defined on line 8 of the following file:
app/helpers/categories_helper.rb
def expandable?
(defined?(@category) && !@category&.id.nil? && !current_page?(new_category_url)) ||
(defined?(@post) && !@post&.category.nil?) ||
(defined?(@question) && !@question&.category.nil?) ||
(defined?(@article) && !@article&.category.nil?)
end
Instance variables with @
A variable that starts with an "at" symbol, such as @post
, is a class instance variable. It is accessible from anywhere inside an instance of the class. In the case of Rails, an instance variable that is defined in the controller is available in the view.
Note that without the leading @
a variable is just local, not accessible from anywhere else. The variable post
is not related to the instance variable @post
.
In order for the expandable?
function to return true, you could either define @post
in the controller before it renders the view, or you could modify the expandable?
function to also look for @edit
. For example, you could add an extra condition at the end to give the following:
def expandable?
(defined?(@category) && !@category&.id.nil? && !current_page?(new_category_url)) ||
(defined?(@post) && !@post&.category.nil?) ||
(defined?(@question) && !@question&.category.nil?) ||
(defined?(@article) && !@article&.category.nil?) ||
(defined?(@edit) && [email protected]&.category&.nil?)
end
This should cause expandable?
to return true for a suggested edit page, but presumably the view will still be looking for @post
, @question
, or @article
to find out what category to display, so you might need to make it also accept @edit
.
0 comment threads