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> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 18 November 2012 15:29, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> > 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> > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 18 November 2012 15:35, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 November 2012 15:29, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> 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> > Colin > >> >> 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> >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit https://groups.google.com/groups/opt_out. >> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 18 November 2012 15:37, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 November 2012 15:35, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 18 November 2012 15:29, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> 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.Have a look at the Rails Guides on Layouts and Rendering to see how to use partials. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.