Topic: How to display data from model help?

I have book on rails and looked at several documentation.  I am confused on how to grab the data in my model to display in a view.  The steps I took:

I have a SQLite3 db that I created with only one row.
columns - first_name middle_name last_name
All three column in my only row are filed in with data.

rails new app_name
cd app_name
changed yml path to a database I have already created in SQLite3
rails generate controller User  index
rails generate model User first_name:string middle_name:string last_name:string
rake db:migrate

Now I am lost on how to grab the data in the model to display in view.

I tried to use below:

def index
      @names = User.find(:all)
end

in my view I place the <%= @names %> and I only see a bracket.  If my file extension to my database displays a prefix .db, does that make my adapter: sqlite3 wrong?

Last edited by Lluvain (2012-07-17 08:28:13)

Re: How to display data from model help?

I'd suggest you experiment with "rails generate scaffold [model] [field1:type1] ...." instead of the generate steps you named. Then migrate, run rails server, look at the ui, and the generated code. This will generate the "guts" of the views in files with the right extensions. It should all become a lot more clear using that approach.

I'm a Phoenix Ruby on Rails developer currently available for freelance work.

Re: How to display data from model help?

Hi elonf.  I will give it a try. I was not sure that scaffolding could be used when connecting to an existing database.

I dont know if I did something wrong but this is what I am getting. 

My pre-created SQLite database has a table user with columns first_name, middle_name, and last_name.
I have prefilled data for four rows.
I created my new project
Changed the .yml file to linux path /home/noob/Documents/Database/TestDatabase.sqlite for development, test, and production.
Saved project and ran my scaffold to mimic my SQLite columns as strings.
ran db:migrate and then ran the server.
The page comes up with no errors however there is no data shown and when I add a new user it saves but not to the database.  I believe rails is savign it to its default database created when I used scaffold.

Last edited by Lluvain (2012-07-18 19:17:16)