How to access an element from a instance variable

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

On Sat, Nov 26, 2011 at 3:00 PM, brent brent [email protected]
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 S. ------------------------ [email protected]main.invalid
http://about.me/hassanschroeder
twitter: @hassan

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 thanks

Thanks 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs