so you have to iterate through this array
@users.each do |user|
puts user.name
end
tom
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> 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
www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
===============================================================================
thanksOn Mon, Jun 20, 2011 at 12:38 PM, Everaldo Gomes <everald...@gmail.com> wrote:
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
On Mon, Jun 20, 2011 at 1:33 PM, Yennie <joann...@gmail.com> wrote: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
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
--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zIp7YSc9B_MJ.
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
<tr width = "50%">
<td><%= image_tag url_for(:controller => "/users", :action => "index"), :width => "25px", :height => "25px"%></td>
<%@pic.each do |c|%>
<%= c.image%>
<% end %>
error is
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/B259TKI0970J.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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!endby 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.
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.
def index
@users= User.all
@pic= []
@users.each do |p|
@pic<< Picture.where(:phrase_id => :route , :culture_id => p.culture_id).first
end
@pic.compact!
send_data @pic.image, :type => 'image/png', :disposition => 'inline'
end
>> def index
>> @users= User.all
>> @pic= []
You would probably have fewer problems if you observed the Rails
naming conventions; e.g.
@pictures = [] # plural
@users.each do |user|
@pictures << Picture.where(:phrase_id => :route , :culture_id
=>user.culture_id).first
end
@pictures.compact!
Now it's obvious that the next line makes no sense:
send_data @pictures.image, :type => 'image/png', :disposition
=> 'inline'
An *array* of pictures doesn't have an "image" attribute.
1. Remove the send_data line, it isn't required.
2. Can you check if there are any records in your pictures table? Right now it looks like it is returning nil.
I think it might be a good idea for you to work through some Rails
tutorials to understand the basics of Rails. The free-to-use-online
tutorial railstutorial.org is good. It might seem that the tutorial
does not lead to the application that you want to develop but if you
work right through it you will learn a vast amount and will then be
embarrassed to look back here at the questions you were asking :)
Also take a good look at the Rails Guides. I mean a *good* look do
not just skim through them. Again you will learn a lot and the time
spent will be saved many times over.
Colin
Not sure why it errors out now.Do you get the same error is it a different error this time?
Oh, so you are trying to get the image url from the user's index action?If yes, that's not correct.
You should instead have a different method name and should request for image url of only one specific user.
It would help in cleaning up the code and solve this issue faster, if you can tell me what exactly you are trying to do.
>> def index
>> @users= User.all
>> @pic= []
You would probably have fewer problems if you observed the Rails
naming conventions; e.g.
@pictures = [] # plural
@users.each do |user|
@pictures << Picture.where(:phrase_id => :route , :culture_id
=>user.culture_id).first
end
@pictures.compact!
Now it's obvious that the next line makes no sense:
send_data @pictures.image, :type => 'image/png', :disposition
=> 'inline'
An *array* of pictures doesn't have an "image" attribute.
HTH,
Ok, that's clear now.Do you use pastie (http://pastie.org/) or gist (https://gist.github.com/)?
If yes, can you paste following on any of them and reply with link?
No, I am using that..
#modelsUser
Picture
#controllersUsersController
#viewsusers/index
Ok, that's clear now.Do you use pastie (http://pastie.org/) or gist (https://gist.github.com/)?
If yes, can you paste following on any of them and reply with link?
No, I am not using that..
Ok, some more questions:1. So, you want to display an image of the culture associated with the user, right?
2. How are you uploading pictures to your database?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yC0telfib3kJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/amryLvSd4UEJ.