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

  • SEARCH
  • Home
  • 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 7931555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:45:16+00:00 2026-06-03T20:45:16+00:00

I am building a simple test to display product for a user. My spec

  • 0

I am building a simple test to display product for a user. My spec looks like this:

require 'spec_helper'

describe "Show Products" do
  it "Displays a user's products" do
    product = Factory(:product)
    visit products_path
    page.should have_content("ABC1")
  end
end

and my factory for product looks like this:

FactoryGirl.define do
  factory :product do
    sequence(:identifier, 1000) {|n| "ABC#{n}" }
  end
end

I have a simple view:

<table id="products">
  <thead>
<th>Product ID</th>
  </thead>
  <tbody>
    <% for product in @products %>
      <tr>
        <td><%= @product.identifier %></td>
      </tr>
    <% end %>
  </tbody>
</table>

The error I’m getting is that there is no such thing as @products. Well, yeah. And that’s my question. Since my factory is defined as “product”, and there is a sequence in it, how do I put the values from “product” into a variable called “products”.

I am basically confused by the FactoryGirl syntax above. How can I have multiple products generated on that one line, yet the factory name has to match the model?

  • 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-06-03T20:45:18+00:00Added an answer on June 3, 2026 at 8:45 pm

    The instance variable @products is most likely assigned in the index action of your ProductsController, or it probably should be defined there if it’s not.

    Generally what happens in request specs is that you use a Factory to create an object that is persisted in the database, and the controller subsequently retrieves those records and assigns them to an instance variable that is made available to the view. Since it looks like you’re rendering an index, I would expect to see something like this in your controller:

    class ProductsController < ApplicationController::Base
      def index
        @products = Product.all
      end
    end
    

    This instance variable will be available to the view when it is rendered.

    Also, it looks like you have a typo in your view. In the iterator you have:

    for product in @products
      # do something with product
    end
    

    This is going to iterate over every one of the products, and make the variable ‘product’ available within the block. Instead, you’re using @product in the block, which seems to be a typo.

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

Sidebar

Related Questions

I'm building a very simple ASP.NET user control to display messages with the help
As part of a test bench I'm building, I'm looking for a simple class
I'm building a simple app and trying to test DB result output. But unfortunately
I'm building a simple NVelocity template but I can't figure out how to test
I'm building a simple Flex App for mobile to test things out. Is it
I'm building a simple audio player in AS3. I would like to embed multiple
I'm building a simple ice sliding puzzle to test uses of allegro. So far
I am building a very simple WCF 4 service to test SharePoint External content
I've stumbled over a seemingly simple problem while building two libraries and test programs
Building a simple asp.net web app just to test things out (will obviously refactor

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.