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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:06:39+00:00 2026-05-15T16:06:39+00:00

This question relates to cleaning up the view and giving the controller more of

  • 0

This question relates to cleaning up the view and giving the controller more of the work.

I have many cases in my project where I have nested variables being displayed in my view. For example:

# controller
@customers = Customer.find_all_by_active(true)
render :layout => 'forms'

# view
<% @customers.each do |c| %>
  <%= c.name %>
  <% @orders = c.orders %>  # I often end up defining nested variables inside the view
    <% @orders.each do |o| %>
    ...
    <% end %>
<% end %>

I am fairly new to RoR but it seems that what I’m doing here is at odds with the ‘intelligent controller, dumb view’ mentality. Where I have many customers, each with many orders, how can I define these variables properly inside my controller and then access them inside the view?

If you could provide an example of how the controller would look and then how I would relate to that in the view it would be incredibly helpful. Thank you very much!

  • 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-15T16:06:40+00:00Added an answer on May 15, 2026 at 4:06 pm

    I don’t think there is anything drastically wrong with what you’re doing. Looping through the customers and outputting some of their attributes and for each customer, looping through their orders and outputting some attributes is very much a view-oriented operation.

    In the MVC architecture, the controller has responsibility for interacting with the model, selecting the view and (certainly in the case of Rails) providing the view with the information it needs to render the model.

    You might consider extracting the code into a view helper though, if you have that exact code repeated more than once. You could even genericize it, passing in the name of a model and association. I haven’t tested it, but you should be able to do something like this:

    def display_attributes(models, association, attribute, association_attribute)
      content = ''
      models.each do |m|
        content << "<p>#{m.attribute}</p>"
        associated_models = m.association
        associated_models.each do |am|
          content << "<p>#{am.association_attribute}</p>"
        end
      end
      content
    end
    

    Then in the view, you could use the helper like this:

    <%= display_attributes(@customers, orders, name, name) %>
    

    Obviously you would change the HTML markup within the helper method to suit your requirements. Note that if you’re not using Rails 3 then you’ll want to escape the output of the attribute names in the helper method.

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

Sidebar

Related Questions

This question relates to an ASP.NET website, originally developed in VS 2005 and now
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support
I know this is not a real programming question. But, it relates to programming
This question is related to (but perhaps not quite the same as): Does Django
This question is related to another question which I asked yesterday! List all links
This question is related to a previous post of mine Here . Basically, I
[This question is related to but not the same as this one .] My
This question is related to this one , though I think I was a
[This question is related to but not the same as this one .] If
This question is related to the question posted here: Why isn't my custom WCF

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.