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 6829037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:27:12+00:00 2026-05-26T22:27:12+00:00

Using rails 3.1, ruby 1.92. I have a bunch of products in a store:

  • 0

Using rails 3.1, ruby 1.92. I have a bunch of products in a store: movies, music, and books. I have three scopes in my products controller, all called the same (movies, music, and books). The point of our assignment is to have the index of the product controller show all items on load of the site. No problem. We then have to link_to books, music, or movies, and have the appropriate products shown. Also done.

However, I now have three files in my views, and I need to make a partial to display all of these. I have used this:

<% @products.each do |product| %>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%= product.title %></h3>
    <%= sanitize(product.description) %>
    <div class="price_line">
      <span class="price"><%= number_to_currency(product.price) %></span>
      <%= button_to 'Add to Cart', line_items_path(:product_id => product) %>
    </div>
  </div>
<% end %>

I don’t really understand what is happening. I have taken what is originally a ‘@product’ and with the use of my scope, it is now @book, or @music, etc. In my code with the three files, I have this code, but instead of ‘@products’ and ‘do |product|’, I have @books do |book| etc.

What do I need to do so the partial will be able to be functional using @books, @music, and @movies? Each time I pass these variables I get the NilClass error. How do I go about this?

  • 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-26T22:27:12+00:00Added an answer on May 26, 2026 at 10:27 pm

    Check out the rails guide on layouts and rendering – especially the section on partials, which I linked you directly to.

    Basically, you want to refactor the code to avoid using @products, @books, @movies, etc, and make a partial that uses a local variable.

    In your products view:

    <%= render :partial => "whatever_you_name_it", :locals => {:products => @products} %>
    

    In your books view:

    <%= render :partial => "whatever_you_name_it", :locals => {:products => @books} %>
    

    In your movies view:

    <%= render :partial => "whatever_you_name_it", :locals => {:products => @movies} %>
    

    And inside the partial, just change your @products.each do |product| to products.each do |product|

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

Sidebar

Related Questions

I have a deployment using: rails 2.3.2 ruby 1.8.7 mysql db and 3 mongrel
We're using Rails 3.2.3, Ruby 1.9.1, and HAML. We have a Post model and
I am using Ruby on Rails 3.2.2 and MySQL. I have a method (an
I am using Ruby on Rails 3.2.2 and ruby 1.9 . I do have
I have created a blog application using Ruby on Rails which includes the ability
I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1, capybara-1.1.2 and factory_girl-2.6.3. I have
I have built a very simple blog application using Ruby on Rails. New to
we have a mixed development environment using ASP.NET MVC and Ruby on Rails. We
I have a Rails 3.2.2 application using Ruby version 1.9.2. I have created basic
I am using Ruby (1.9.3) and Rails (3.2.2). I have tasks file which contains

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.