Get DB data to rhtml file

I am trying to get data out of a database to my rhtml file. The files
look like this:

Model

mystuff.rb

class Mystuff < ActiveRecord::Base
set_table_name “mytable”
set_primary_key “id”
end

Controllers

mystuff_controller.rb

require ‘Mystuff’

class MystuffController < ApplicationController

def read

   @me =  Mystuff.find( :all)

end

end

Index

Index.html.erb

Mystuff#index

<%mystuff.each do |mr|%>

mr.m1

<% end %>

Thanks for you help on yet another stupid newbie question.

Mark

On Jul 12, 11:47 pm, Mark P. [email protected]
wrote:

Mystuff#index

<%mystuff.each do |mr|%>

mr.m1

<% end %>

if you want to display the result of a ruby expression (eg one of your
attributes) then you need to use <%= … %> (don’t forget to use h to
escape nasties (if appropriate))

Fred

Frederick C. wrote:

On Jul 12, 11:47�pm, Mark P. [email protected]
wrote:

Mystuff#index

<%mystuff.each do |mr|%>

mr.m1

<% end %>

if you want to display the result of a ruby expression (eg one of your
attributes) then you need to use <%= … %> (don’t forget to use h to
escape nasties (if appropriate))

Fred

When I run the webrick server I get the following error message:

Showing app/views/mystuff/index.html.erb where line #2 raised:

undefined local variable or method `mystuff’ for
#ActionView::Base:0x388b348

Extracted source (around line #2):

1:

Mystuff#index


2:

<%mystuff.each do |mr|%>


3:

=mr.m1


4: <% end %>

HELP :slight_smile:

Mark P. wrote:

end

Index.html.erb

Mystuff#index

<%mystuff.each do |mr|%>

mr.m1

<% end %>

In addition to Fredericks point, you might also want to check the
validity of the html that will output.

The closing

tag is inside the loop and thus will be repeated for
each element:

Mystuff#index

Stuff1

Stuff2

Stuff3

And so on. Hope this sheds a little light.

Matt

Mark P. wrote:

escape nasties (if appropriate))

Extracted source (around line #2):

1:

Mystuff#index


2:

<%mystuff.each do |mr|%>


3:

=mr.m1


4: <% end %>

HELP :slight_smile:

Ok, in the controller you are assigning @me to your find. Try:

<% @my.each do |mr| %>

in your view instead.

Also, don’t forget to enclose that attribute in tags:

<%= mr.m1 %>

HTH

Matt

Matt H. wrote:

Mark P. wrote:

escape nasties (if appropriate))

Extracted source (around line #2):

1:

Mystuff#index


2:

<%mystuff.each do |mr|%>


3:

=mr.m1


4: <% end %>

HELP :slight_smile:

Ok, in the controller you are assigning @me to your find. Try:

<% @my.each do |mr| %>

in your view instead.

Also, don’t forget to enclose that attribute in tags:

<%= mr.m1 %>

HTH

Matt

Thanks for the reply!

I modified the controller to look like this:

require ‘Mystuff’

class MystuffController < ApplicationController

def read

   @my =  Mystuff.find( :all)

end

end

and the view to look like this:

Mystuff#index

<%me.each do |mr|%>

<=%mr.m1%>

<% end %>

and am getting the following error:

undefined local variable or method `me’ for
#ActionView::Base:0x381bb24

Extracted source (around line #2):

1:

Mystuff#index


2:

<%me.each do |mr|%>


3:

<=%mr.m1%>


4: <% end %>

Thanks for the continued help !!!

On Jul 13, 12:38 am, Mark P. [email protected]
wrote:

   @my =  Mystuff.find( :all)

This

Mystuff#index

<%me.each do |mr|%>

and this need to match - the instance variables are copied over for
you.

Fred

Mark P. wrote:

Matt H. wrote:

Mark P. wrote:

escape nasties (if appropriate))

Extracted source (around line #2):

1:

Mystuff#index


2:

<%mystuff.each do |mr|%>


3:

=mr.m1


4: <% end %>

HELP :slight_smile:

Ok, in the controller you are assigning @me to your find. Try:

<% @my.each do |mr| %>

in your view instead.

Also, don’t forget to enclose that attribute in tags:

<%= mr.m1 %>

HTH

Matt

Thanks for the reply!

I modified the controller to look like this:

require ‘Mystuff’

class MystuffController < ApplicationController

def read

   @my =  Mystuff.find( :all)

end

end

and the view to look like this:

Mystuff#index

<%me.each do |mr|%>

<=%mr.m1%>

<% end %>

and am getting the following error:

undefined local variable or method `me’ for
#ActionView::Base:0x381bb24

Extracted source (around line #2):

1:

Mystuff#index


2:

<%me.each do |mr|%>


3:

<=%mr.m1%>


4: <% end %>

Thanks for the continued help !!!

ok, cleaned up some of my silly errors view looks like this now:

Mystuff#index

<%my.each do |mr|%>

<%=mr.m1%>

<% end %>

but still getting the error:

undefined local variable or method `my’ for
#ActionView::Base:0x381bb24

Extracted source (around line #2):

1:

Mystuff#index


2:

<%my.each do |mr|%>


3:

<%=mr.m1%>


4: <% end %>

Mark P. wrote:

Frederick C. wrote:

On Jul 13, 12:38�am, Mark P. [email protected]
wrote:

� � � �@my = �Mystuff.find( :all)
This

Mystuff#index

<%me.each do |mr|%>

and this need to match - the instance variables are copied over for
you.

Fred

Fred thanks, I saw that and changed it, but still am getting the error:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #2):

1:

Mystuff#index


2:

<[email protected] do |mr|%>


3:

<%=mr.m1%>


4: <% end %>

and I checked the DB, there is definately data in m1

DB

id m1
1 45
2 452

Thanks

I got it to work by changing my method in the mystuff_controller to look
like this:

require ‘Mystuff’

class MystuffController < ApplicationController

def index

   @my =  Mystuff.find( :all)

end

end

The only change was to change the name of the method to “index” Seems
odd

Thanks all

Mark

On Jul 13, 12:55 am, Mark P. [email protected]
wrote:

end

end

The only change was to change the name of the method to “index” Seems
odd

Seems like you were hitting the index action (ie going to /mystuff )
so your read method was never getting called.

Fred

Mark P. wrote:

I got it to work by changing my method in the mystuff_controller to look
like this:

require ‘Mystuff’

class MystuffController < ApplicationController

def index

   @my =  Mystuff.find( :all)

end

end

The only change was to change the name of the method to “index” Seems
odd

Thanks all

Mark

This seems like a perfect place to run:

ruby script/generate scaffold widget name:string quantity:integer

then take a look at the whole tree of items that the scaffold generator
creates for you. There are some nice instructional clues in how rails
likes to operate in that generated code…
the standard routes (rake routes >routes.lst), controller methods, and
views show you how rails likes to work:
look at routes.lst…

widgets GET /widgets :controller => “widgets”, :action => “index”

listing widgets -> widgets_controller -> index method -> index view,

new_widget GET /widgets/new :controller => “widgets”, :action =>
“new”

create a widget -> widgets_controller -> new method -> new view

I scaffold ‘proof-of-concept’ enhancements all the time in ‘co-operative
design’ sessions for users to get a quick look at their new requested
feature… it doesn’t have all the layout finery added (scaffold forms
are ugly, but they work), but in the dev environment, that’s fine. We
have some very visual thinkers among our users…

Frederick C. wrote:

On Jul 13, 12:38�am, Mark P. [email protected]
wrote:

� � � �@my = �Mystuff.find( :all)
This

Mystuff#index

<%me.each do |mr|%>

and this need to match - the instance variables are copied over for
you.

Fred

Fred thanks, I saw that and changed it, but still am getting the error:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #2):

1:

Mystuff#index


2:

<[email protected] do |mr|%>


3:

<%=mr.m1%>


4: <% end %>

and I checked the DB, there is definately data in m1

DB

id m1
1 45
2 452

Thanks