Question

Say I have a projects controller

with an @project instance variable

and I also have a project_updates controller

in my project_updates/views/show can I call on the @project variable? For example I have categories for my @project instances

In a project_updates show view can I call on:

<% @project.categories.each do |cat| %>
  <li><%= cat.name %></li>
<% end %>

Or do I need to bring the instance variable from a different controller into the other controller in some way?

Was it helpful?

Solution

I may be over simplifying this, but while you may think you have a variable in another controller, that may not be true.

When an request comes in and is routed to a controller, an instance of the controller is created. If that other controller is not some how instantiated as part of the request then that variable you think is there is not really there.

Models however do exist (at least in database form). So Basically I think you should be trying to get that @project variable created in your project_updates controller by extracting some data from your model.

How do you create the @project variable in the other controller? Most likely you would use the same approach in this controller to create it, then it would be available to your view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top