Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

How does a view know it should show the category bar? Question

+1
−0

Pages that are "in" a category show the category bar at the top, like this:

suggested edits page with Q&A: posts, tags, edits

An individual suggested edit should logically be "in" a category too, but isn't shown that way:

category bar is missing

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Works for me (1 comment)
Curiouser and curiouser (1 comment)

Sign up to answer this question »