1

So I have two models & controllers. Projects and Designers.

In my designers index view, I want it to show a list of all the projects that the designer has.

However, when I do a simple query like this:

<% @projects.each do |project| %>
  <tr>
    <td><%= link_to 'Show', project %></td>
    <td><%= link_to 'Edit', edit_project_path(project) %></td>
    <td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>

From the index.html.erb in the Designers view, it gives me the following error:

NoMethodError in Designers#index
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

However, when I run that EXACT code from the index.html.erb file in the projects view, it works perfectly.

So how am I able to access that controller or at least data from the view of another controller? Do I have to add a projects variable (where it queries the db) to my index object in my designers controller?

Thanks.

2
  • can you link the models and controllers? Commented Nov 14, 2010 at 7:31
  • @r-dub, do you mean posting them here or can I link them in Rails? Commented Nov 14, 2010 at 7:41

3 Answers 3

4

UPDATED TO USE A SINGLE DESIGNER RECORD

You should use associations. Add this to your models.

class Designer < ActiveRecord::Base
  has_many :projects
end

class Project < ActiveRecord::Base
  belongs_to :designer
end

Your view should look like this:

<% @designer.projects.each do |project| %>  

<% end %>

More info on associations here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Sign up to request clarification or add additional context in comments.

2 Comments

What I really wanted is for on each designers 'index' page, i.e. once they login, they see a list of all of their projects. So it's not exactly like you have in this example. If I am not mistaken, the functionality you have added here essentially allows someone with permission to view all the designers and all their projects, right? As opposed to one designer viewing all their projects.
Typically you would use the index action to show a list of the model objects, and you would use the show action for a single object, so I thought you wanted a list here. Answer updated to use a single record.
1

Figured out one way to do it. All I had to do was add an instance variable in my designers controller:

@projects = Project.all

But...that's not very DRY. Does anyone have a more elegant 'DRY' solution, so if I want to access other variables in other controllers I can do that easily without having to re-create them in the current controller?

Thanks.

1 Comment

@cowboycoded....you are right. That is not my intent. I want only the projects associated with the designer logged in. Not all the projects on the system.
0

In your designers controller, you need to set the @projects instance variable to only those projects belonging to the signed in designer, correct?

You'll need something like this:

def index

  @projects = Project.where(:user_id => where ever you've stored your userid) 

end

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.