Folks im new to rails and was wondering how i would access these elements from my view in my controller i have @data = Editpatient.find(:all, :conditions => ["patient_id = ?", params[:patients]] ) and in the view i have <%= @data %> this displays all the data but i only want one field. This is what is displayed. [#<Editpatient id: 27, patient_id: 14, provider_id: 11, weight_gained: 123, weight_lost: nil, created_at: "2011-11-26 21:07:13", updated_at: "2011-11-26 21:07:13", patient_name: nil, provider_name: nil>] the only field i want is patient_id ive tried this in the view @data[:patient_id] and i get can''t convert Symbol into Integer any help is appreciated 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 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Nov-26 23:42 UTC
Re: how to access an element from a instance variable
On Sat, Nov 26, 2011 at 3:00 PM, brent brent <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @data = Editpatient.find(:all, :conditions => ["patient_id = ?", > params[:patients]] )erm, well. If you expect to get back one record for a given patient_id it could be more readably expressed as: @editpatient = Editpatient.find_by_patient_id(params[:patient_id]) (assuming you fix your form to pass ''patient_id'').> and in the view i have <%= @data %>Which is opaquely named, and hides the fact that you''re fetching an array.> this displays all the data but i only want one field. This is what is > displayed.@data.first.patient_id would do it, but I''d strongly recommend cleaning up the code to make your intent more apparent, and only fetch one record, e.g. @editpatient.patient_id HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder 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 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
brent brent wrote in post #1033872:> Folks im new to rails and was wondering how i would access these > elements from my view > > > in my controller i have > @data = Editpatient.find(:all, :conditions => ["patient_id = ?", > params[:patients]] ) > > and in the view i have <%= @data %> > > this displays all the data but i only want one field. This is what is > displayed. > > [#<Editpatient id: 27, patient_id: 14, provider_id: 11, weight_gained: > 123, weight_lost: nil, created_at: "2011-11-26 21:07:13", updated_at: > "2011-11-26 21:07:13", patient_name: nil, provider_name: nil>] > > the only field i want is patient_id > > ive tried this in the view @data[:patient_id] and i get > can''t convert Symbol into Integer > > any help is appreciated thanksThanks for the help Hassan This is just a test app that im using to get the hang of ruby and rails. Your method worked perfectly. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.