Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:33:44+00:00 2026-05-23T11:33:44+00:00

I have a Rails 3 partial that lists all categories as a navigation menu

  • 0

I have a Rails 3 partial that lists all categories as a navigation menu – it’s on most, but not all of my template pages…let’s say about 75%. I’m trying to test the partial (in RSpec) right now, and I’ve just realised a few things:

At the moment, I’m calling Categories.all in the actual view. The difficulty is that, because that touches the database, my mocks/stubs in the view spec are ignored, and consequently the test fails.

I’m guessing the alternative is to assign the variable in the application controller, and then pass it as a local variable to the partial. Still, about 25% of my pages won’t use the variable and I’m wondering if there’s a more graceful way of doing things.

In short, I want view specs to pass without touching the test DB, but I’m not sure a global variable passed to my partial is the best way to do it…and I’m not declaring the variable in every (& only) those controllers who require it.

Any suggestions appreciated…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:33:44+00:00Added an answer on May 23, 2026 at 11:33 am

    Why not create a helper method for all categories?

      # in categories helper
    
    def all_categories
      @all_categories ||= Category.all
    end
    

    Or…

    # application controller
    
    helper_method :all_categories
    
    def all_categories 
    ...
    
    

    You can then stub out this method in your specs and you won’t be touching the DB

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a _form.html.erb partial that comes from the standard rails 3 template for
Specifically, I have a number of pages in my Rails app that use the
I have a rails app that has picked up a bit of traction, but
I have a rails partial that renders a sidebar for users. As part of
I have several partials that I'm including in my Rails application.html.erb file , but
I have an Rails 3.1 app that uses template inheritance and that works great:
I have a partial in my rails app that loads the vote form (It's
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a rails app that I have serving up XML on an infrequent
I have a nested form in a rails view that is called like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.