Aaron Robeson
Aaron Robeson

Reputation: 312

How to call data from a table associated with a different controller

I'm kind of new to RoR so this may be a stupid question, but is there any way to pull data associated with one controller into a form in the view of another?

I have 2 controllers, home and subjects and I have one model subject (probably a poor choice in hindsight). I have a table in my database called subjects and a column in that table called text I want to pull data from that column into both my home and subjects views and i can do it fine in the subjects view but i can't get it to work in the home view.
I'm doing it with

def index
@subjects = Subject.order("subjects.position ASC")
end

in the subjects controller, and

<tr>
<th>Text</th>
<td><%= @subject.Text %></td>
</tr>

in the view.

Let me know if you need any more information, Thanks.

Upvotes: 0

Views: 165

Answers (2)

stephenmurdoch
stephenmurdoch

Reputation: 34643

put this in your view

<% for subject in @subjects %>
  <tr>
    <th>Text</th> 
    <td><%= subject.text %></td> 
  </tr> 
<% end %>

Note that I am using subject as opposed to @subject inside the loop, and that .text is not capitalised as in your code

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160271

Controllers are only associated to tables by convention.

Models are associated to tables, and you can use whatever models you want in your controllers.

Upvotes: 0

Related Questions