Hi all, I have very basic error that i have no idea how to show
I have user table
id
name
add
phone number
then i select all in user table
using => @users = User.all
=> @users.name
it gives me undefined method name Array<…>
Please give me some advice…
Thanks
Yennie
June 20, 2011, 6:40pm
2
Hi!
The method User.all returns an Array of users, not a single user.
I think you have to iterate over the array, using each.
@users.each { |u| u.name }
Everaldo
Yennie
June 20, 2011, 6:40pm
3
@users.all basically returns “array” of users
so you have to iterate through this array
@users.each do |user|
puts user.name
end
tom
On Jun 20, 2011, at 18:33 , Yennie wrote:
using => @users = User.all
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
Yennie
June 20, 2011, 6:50pm
4
thanks
On Mon, Jun 20, 2011 at 12:38 PM, Everaldo G.
[email protected] wrote:
Thanks
–
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Yennie
June 20, 2011, 8:43pm
5
On 20 June 2011 17:53, joanne ta [email protected] wrote:
and it causes undefined method image
What does it say has not got a method image? It is always a good idea
to post the complete error message (use copy/paste rather than
re-typing it). If it says the nil has not got the method then your
call of Picture.where has not found a record.
Colin
Yennie
June 20, 2011, 6:55pm
6
I have other undefined method when i combine the code above together
<%@users = User.all%>
<[email protected] do |u|%>
<%u.culture_id%>
<% @pic= Picture.where(:phrase_id => :route , :culture_id =>
u.culture_id).first%>
My picture <%= @pic.image %>
and it causes undefined method image
please give me advices… thank you so much
Joanne
On Mon, Jun 20, 2011 at 12:48 PM, joanne ta [email protected]
wrote:
id
it gives me undefined method name Array<…>
Please give me some advice…
Thanks
Joanne
Yennie
June 20, 2011, 9:09pm
7
the error is undefined method `image’ for nil:NilClass
On Mon, Jun 20, 2011 at 2:42 PM, Colin L. [email protected]
wrote:
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Yennie
June 20, 2011, 9:35pm
8
You don’t have an <% end %> to close the <% @users.each %> block in the
code
you included, so my guess is that your u variable is already out of
scope by
the time you call u.image.
Yennie
June 21, 2011, 9:25am
10
On 20 June 2011 20:08, joanne ta [email protected] wrote:
Please don’t top post, it makes it difficult to follow the thread.
Insert your reply at appropriate places in previous message. Thanks.
the error is undefined method `image’ for nil:NilClass
So what does that mean (see my previous post)?
Colin
Yennie
June 21, 2011, 3:53pm
11
end
<% end %>
error is
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
Extracted source (around line #79 ):
76:
77: <%= image_tag url_for(:controller => “/users”, :action =>
“index”),
:width => “25px”, :height => “25px”%>
78:
79: <[email protected] do |c|%>
80: <%= c.image%>
81: <% end %>
82:
please give me some advices…
Yennie
June 21, 2011, 3:48pm
12
My controller:
def index
@users= User.all
@users.each do |p|
@pic= Picture.where(:phrase_id => :route , :culture_id =>
p.culture_id).first
send_data @pic.image , :type => ‘image/gif’, :disposition =>
‘inline’
end
end
in View
<%= image_tag url_for(:controller => "/users", :action => "index"),
:width => "25px", :height => "25px"%>
<[email protected] do |c|%>
<%= c.image%>
<% end %>
please give me some advices…
thanks
Joanne
Yennie
June 21, 2011, 4:14pm
13
it does not work, it is complaining other error
NoMethodError (undefined method image' for [nil]:Array): app/controllers/patients_controller.rb:27:in
block in index’
app/controllers/patients_controller.rb:25:in each' app/controllers/patients_controller.rb:25:in
index’
On Tue, Jun 21, 2011 at 9:57 AM, Chirag S.
[email protected] wrote:
end
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Yennie
June 21, 2011, 3:59pm
14
Your index action should be something like this:
def index
@users= User.all
@pic = []
@users.each do |p|
@pic << Picture.where(:phrase_id => :route , :culture_id =>
p.culture_id).limit(1)
end
end
Yennie
June 21, 2011, 4:29pm
15
Oh yes… sorry about that.
I assumed that you will find picture for every query.
You can try this instead:
def index
@users= User.all
@pic = []
@users.each do |p|
@pic << Picture.where(:phrase_id => :route , :culture_id =>
p.culture_id).limit(1)
end
@pic.compact !
end
by calling compact! on the array, we will eliminate all nil object.
On another note, are you sure, this is what you want to do? If you have
100
users, it will fire 100 sql queries which is not good.
On Tue, Jun 21, 2011 at 7:42 PM, joanne ta [email protected] wrote:
end
For more options, visit this group at
Yen
–
Chirag
http://sumeruonrails.com
Yennie
June 21, 2011, 4:58pm
16
Can you paste what you have in your index method?
You should not be getting this error because we have already defined
@pic as
an empty array.
On Tue, Jun 21, 2011 at 8:10 PM, joanne ta [email protected] wrote:
@users= User.all
The error occurred while evaluating nil.compact!):
On Tue, Jun 21, 2011 at 7:42 PM, joanne ta [email protected] wrote:
On Tue, Jun 21, 2011 at 9:57 AM, Chirag S. <
p.culture_id).limit(1)
To unsubscribe from this group, send email to
Thank you,
For more options, visit this group at
You received this message because you are subscribed to the Google G.
–
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Chirag
http://sumeruonrails.com
Yennie
June 21, 2011, 4:42pm
17
On Tue, Jun 21, 2011 at 10:27 AM, Chirag S.
[email protected] wrote:
p.culture_id).limit(1)
end
@pic.compact !
end
by calling compact! on the array, we will eliminate all nil object.
On another note, are you sure, this is what you want to do? If you have 100
users, it will fire 100 sql queries which is not good.
Yes i think i have that problem as well because if i remove "@users.each
do |p| "
and then it will cause undefined method of culture_id…
plus when i call @pic.compact ! , it gives me
NoMethodError (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.compact!):
app/controllers/patients_controller.rb:29:in `index’
what is mean? cuz i want to display picture on the browser too…
please help… thank you very much
Joanne
Yennie
June 21, 2011, 5:06pm
18
Can you paste what you have in your index method?
You should not be getting this error because we have already defined @pic
as an empty array.
@pic.compact!
p.culture_id).limit(1)
do |p| "
please help… thank you very much
app/controllers/patients_controller.rb:27:in `block in index’
def index
You received this message because you are subscribed to the Google
[email protected] .
To unsubscribe from this group, send email to
–
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Yennie
June 21, 2011, 5:30pm
19
Remove the send_data line, it isn’t required.
Can you check if there are any records in your pictures table? Right
now
it looks like it is returning nil.
On Tue, Jun 21, 2011 at 8:35 PM, joanne ta [email protected] wrote:
send_data @pic.image, :type => 'image/png', :disposition => 'inline'
p.culture_id).limit(1)
do |p| "
please help… thank you very much
app/controllers/patients_controller.rb:27:in `block in index’
def index
You received this message because you are subscribed to the Google
To unsubscribe from this group, send email to
To post to this group, send email to [email protected] .
http://sumeruonrails.com
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Chirag
http://sumeruonrails.com
Yennie
June 21, 2011, 5:33pm
20
@users.each do |user|
An array of pictures doesn’t have an “image” attribute.
because i want to take a value “image” from Picture model
what can i do … I am really stuck now…
thanks
Joanne
Hassan S. ------------------------ [email protected]
twitter: @hassan
–
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 [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .