Forum: Ruby on Rails newbie q on displaying a db field value in the view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vince W. (Guest)
on 2006-05-11 01:04
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
Alex W. (Guest)
on 2006-05-11 02:07
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:
http://www.digitalmediaminute.com/article/1816/top...

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
<p>Greetings <%= @user.name %>!</p>
Jason (Guest)
on 2006-05-11 04:41
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 %>
<li><%=h user.name %></li> (name is one of the columns from your
database)
<* end %>
This topic is locked and can not be replied to.