Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

In rails web app, where should I put those structured data related to the view?

Let me be more specific on this. Assume I want to produce a HTML like the following

<div id="btn-group-1">
  <button>Button-1a</button>
  <button>Button-1b</button>
  <button>Button-1c</button>
  <button>Button-1d</button>
</div>
<div id="btn-group-2">
  <button>Button-2a</button>
  <button>Button-2b</button>
</div>
<div id="btn-group-3">
  <button>Button-3a</button>
  <button>Button-3b</button>
  <button>Button-3c</button>
</div>

I want to DRY the code, so I want to store the structure in a array

buttonGroups = []
buttonGroups << {:id => "...", buttons = [....]} 
buttonGroups << {:id => "...", buttons = [....]}
buttonGroups << {:id => "...", buttons = [....]}

My question, should I put this part of code in view? Or should I put this in the controller - create a instance variable and pass it to the view? Or should I put them in the model?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Jiaming Zhang was the answer below helpful? If so, please indicate that by selecting it as Best Answer. If not, please elaborate more so that we could help you further.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I would do it in the controller, make an instance variable (or a few of them) and then use .each structure in the view.