Hi, I''m learning Rails and am getting an error following a query. I think I''m missing something simple and would appreciate guidance. In controller: [code]@pictures = Question.find(session[:question_id]).pictures # Query through the join table[/code] @picture : [#<Picture id: 44, title: "", remote_image_url: nil, contributor: "", created_at: "2013-04-10 11:16:34", updated_at: "2013-04-10 11:16:34", gallery_id: nil, image: "pict4.png">, #<Picture id: 46, title: "", remote_image_url: nil, contributor: "", created_at: "2013-04-12 23:39:18", updated_at: "2013-04-12 23:39:18", gallery_id: nil, image: "pict2.jpg">] In view: <% @pictures.each do |picture| %> <%= image_tag(@picture.image %> <% end %> Error: undefined method `image'' for nil:NilClass Thanks -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
The code in your view should be:
<% @pictures.each do |picture| %>
    <%= image_tag(*picture*.image %>
<% end %>
without *@*
2013/4/12 Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> Hi,
> I''m learning Rails and am getting an error following a query.  I
think
> I''m missing something simple and would appreciate guidance.
>
> In controller:
> [code]@pictures = Question.find(session[:question_id]).pictures # Query
> through the join table[/code]
>
> @picture :
> [#<Picture id: 44, title: "", remote_image_url: nil,
contributor: "",
> created_at: "2013-04-10 11:16:34", updated_at: "2013-04-10
11:16:34",
> gallery_id: nil, image: "pict4.png">, #<Picture id: 46,
title: "",
> remote_image_url: nil, contributor: "", created_at:
"2013-04-12
> 23:39:18", updated_at: "2013-04-12 23:39:18", gallery_id:
nil, image:
> "pict2.jpg">]
>
> In view:
> <% @pictures.each do |picture| %>
> <%= image_tag(@picture.image %>
> <% end %>
>
> Error: undefined method `image'' for nil:NilClass
>
> Thanks
>
> --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
-- 
Ricardo Franco Andrade
( Web | Desktop | Game ) Developer
email: ricardo.krieg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
phone: +55 (86) 9436 0830
linkedin: http://www.linkedin.com/pub/ricardo-franco/61/2b0/857
skype: ricardo.krieg
github: https://github.com/ricardokrieg
twitter: twitter.com/ricardokrieg
facebooK: https://www.facebook.com/krieg.ricardo
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Also I''m not sure what Question is, but your picture model seems to be missing a question_id... so your Question.find(session(:question_id).pictures will always be blank, won''t it? Julian On 13/04/2013, at 10:56 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I''m learning Rails and am getting an error following a query. I think > I''m missing something simple and would appreciate guidance. > > In controller: > [code]@pictures = Question.find(session[:question_id]).pictures # Query > through the join table[/code] > > @picture : > [#<Picture id: 44, title: "", remote_image_url: nil, contributor: "", > created_at: "2013-04-10 11:16:34", updated_at: "2013-04-10 11:16:34", > gallery_id: nil, image: "pict4.png">, #<Picture id: 46, title: "", > remote_image_url: nil, contributor: "", created_at: "2013-04-12 > 23:39:18", updated_at: "2013-04-12 23:39:18", gallery_id: nil, image: > "pict2.jpg">] > > In view: > <% @pictures.each do |picture| %> > <%= image_tag(@picture.image %> > <% end %> > > Error: undefined method `image'' for nil:NilClass > > Thanks > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Ricardo Franco wrote in post #1105491:> The code in your view should be: > > <% @pictures.each do |picture| %> > <%= image_tag(*picture*.image %> > <% end %> > > without *@* >Thank you Ricardo! (Must have been too tired...) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Julian Leviston wrote in post #1105496:> Also I''m not sure what Question is, but your picture model seems to be > missing a question_id... so your > Question.find(session(:question_id).pictures will always be blank, won''t > it? > > JulianThe query is to find all pictures with the question id of session[:question_id] from a join table, so works fine. Thanks you both for reply! Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.