Fatal510
Fatal510

Reputation: 1723

using one controllers actions within another controllers views

How do I use another controllers methods inside of another controllers views?

I have a Users controller and a Worlds controller each with their own model and views. I am trying to use something from the Worlds view within the Users views

<% @worlds.each do |world| %>
 ...
<% end %>

but I am getting "You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each"

this listing of each world works as expected when used within the Worlds views

Upvotes: 1

Views: 434

Answers (1)

fl00r
fl00r

Reputation: 83680

Just define in your action in UsersController your @worlds variabe:

# UsersController
def index
  @worlds = World.all
end

And you should understand, that Models and Controllers are different essences. The are separated. Controller can use any model. Controller is a layer between all models and particular view.

Upvotes: 2

Related Questions