开发者

using one controllers actions within another controllers views

开发者 https://www.devze.com 2023-02-28 14:18 出处:网络
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

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 o开发者_开发问答bject 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消