Share partial among different resources

I am trying to render a file from Subject resource in a User resource view. I am getting error: undefined method `each' for nil:NilClass. Apparently @subject is nil but not sure how to fix this...

Here is User resourve view (users/show.html.erb) <%= render 'subjects/index' %>

Here is subjects_controller def show     @subject = Subject.find(params[:id]) ... end

Here is subjects/index.html.erb (The file I want to render) <% @subjects.each do |subject| %>         <tr>           <td class="hilite_list">              <a href="/books/index_books/<%= subject.id %>"><%= subject.title %> (<%= subject.questions.count %>)</a>           </td>

I am trying to render a file from Subject resource in a User resource view. I am getting error: undefined method `each' for nil:NilClass. Apparently @subject is nil but not sure how to fix this...

Here is User resourve view (users/show.html.erb) <%= render 'subjects/index' %>

Are you just trying to go to that page? If so then use redirect_to rather than render, this will go to the subjects#index action on subjects and then render it as normal. As you have it you are trying to render the view without calling the action, so @subjects is not set up.

If you are trying to do something other than just going to that page then please explain in more detail.

Colin

I am trying to render a file from Subject resource in a User resource view. I am getting error: undefined method `each' for nil:NilClass. Apparently @subject is nil but not sure how to fix this...

Here is User resourve view (users/show.html.erb) <%= render 'subjects/index' %>

Are you just trying to go to that page? If so then use redirect_to rather than render, this will go to the subjects#index action on subjects and then render it as normal. As you have it you are trying to render the view without calling the action, so @subjects is not set up.

If you are trying to do something other than just going to that page then please explain in more detail.

Sorry I have just read the question (and the subject) again, and see what you are trying to do. If you want to show that view within another you will have to setup @subjects in the action, otherwise the view has nothing to show.

Colin

Have a look at the Rails Guides on Layouts and Rendering to see how to use partials.

Colin