# Newbie q on displaying a db field value in the view

**URL:** <https://www.ruby-forum.com/t/newbie-q-on-displaying-a-db-field-value-in-the-view/60182>\
**Category:** Rails\
**Created:** [May 10, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/newbie-q-on-displaying-a-db-field-value-in-the-view/60182 "2006-05-10T23:04:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [May 10, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/newbie-q-on-displaying-a-db-field-value-in-the-view/60182/1 "2006-05-10T23:04:58Z")

</div>

My layout calls a few links like so:

\<%= link\_to '| Home ', :controller =\> ‘search’, :action =\> ‘’%\>  
\<%= link\_to '| Search ', :controller =\> ‘search’, :action =\> ‘’%\>

I would also like to display two values from my user table in the same  
layout right after the search: the contents of the name field, and the  
contents of a integer field called tokens.

Can somebody please guide me or start me off in the correct direction  
please?

Thanks,  
Vince

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [May 11, 2006, 12:07am UTC](https://www.ruby-forum.com/t/newbie-q-on-displaying-a-db-field-value-in-the-view/60182/2 "2006-05-11T00:07:16Z")

</div>

Vince W. wrote:

> My layout calls a few links like so:
> 
> \<%= link\_to '| Home ', :controller =\> ‘search’, :action =\> ‘’%\>  
> \<%= link\_to '| Search ', :controller =\> ‘search’, :action =\> ‘’%\>
> 
> I would also like to display two values from my user table in the same  
> layout right after the search: the contents of the name field, and the  
> contents of a integer field called tokens.
> 
> Can somebody please guide me or start me off in the correct direction  
> please?
> 
> Thanks,  
> Vince

I hihgly recommend you try some tutorials to get familiar with the rails  
basics. Try some of the links here:

> **[Top Ruby on Rails Tutorials - Digital Media Minute](https://www.digitalmediaminute.com/article/1816/top-ruby-on-rails-tutorials)**
>
> A former student asked me a few days ago how I learned Ruby on Rails. The answer was that I simply read a lot of great tutorials. So in the spirit of sharing, here is the tutorial I found most useful: Intro to Ruby on Rails – Scott Chow offers his...

But in answer to your question, you retrieve database rows as  
ActiveRecord object in your controller, then access the created  
variables in your view.

# app/controllers/users\_controller.rb

class UsersController \< ApplicationController  
def search  
@user = User.find(1) #find user with an id of “1”  
end  
end

# app/views/users/search.rhtml

Greetings \<%= @user.name %\>!

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [May 11, 2006, 2:41am UTC](https://www.ruby-forum.com/t/newbie-q-on-displaying-a-db-field-value-in-the-view/60182/3 "2006-05-11T02:41:33Z")

</div>

Vince W. wrote:

> My layout calls a few links like so:
> 
> \<%= link\_to '| Home ', :controller =\> ‘search’, :action =\> ‘’%\>  
> \<%= link\_to '| Search ', :controller =\> ‘search’, :action =\> ‘’%\>
> 
> I would also like to display two values from my user table in the same  
> layout right after the search: the contents of the name field, and the  
> contents of a integer field called tokens.
> 
> Can somebody please guide me or start me off in the correct direction  
> please?
> 
> Thanks,  
> Vince

You’ll find this in those tutorials mentioned, but you’d have a method  
in your controller like:

@users = User.find(:all) - an instance variable (@user), that holds all  
(:all) of the rows from your User model.

then in your view:

\<% for user in @users %\>

- \<%=h user.name %\>
 (name is one of the columns from your database) \<\* end %\>
