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 <h1>Mystuff#index</h1> <p><%mystuff.each do |mr|%></p> <h1>mr.m1</h1> <% end %> Thanks for you help on yet another stupid newbie question. Mark -- Posted via http://www.ruby-forum.com/.
On Jul 12, 11:47 pm, Mark Preston <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > <h1>Mystuff#index</h1> > <p><%mystuff.each do |mr|%></p> > <h1>mr.m1</h1> > <% 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> Thanks for you help on yet another stupid newbie question. > > Mark > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On Jul 12, 11:47�pm, Mark Preston <rails-mailing-l...@andreas-s.net> > wrote: >> >> <h1>Mystuff#index</h1> >> <p><%mystuff.each do |mr|%></p> >> <h1>mr.m1</h1> >> <% 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)) > > FredWhen 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: <h1>Mystuff#index</h1> 2: <p><%mystuff.each do |mr|%></p> 3: <h1>=mr.m1</h1> 4: <% end %> HELP :) -- Posted via http://www.ruby-forum.com/.
Mark Preston wrote:> 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 > > <h1>Mystuff#index</h1> > <p><%mystuff.each do |mr|%></p> > <h1>mr.m1</h1> > <% end %>In addition to Fredericks point, you might also want to check the validity of the html that will output. The closing </p> tag is inside the loop and thus will be repeated for each element: <h1>Mystuff#index</h1> <p></p> <h1>Stuff1</h1> </p> <h1>Stuff2</h1> </p> <h1>Stuff3</h1> And so on. Hope this sheds a little light. Matt
Mark Preston wrote:> Frederick Cheung wrote: >> On Jul 12, 11:47�pm, Mark Preston <rails-mailing-l...@andreas-s.net> >> wrote: >>> <h1>Mystuff#index</h1> >>> <p><%mystuff.each do |mr|%></p> >>> <h1>mr.m1</h1> >>> <% 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: <h1>Mystuff#index</h1> > 2: <p><%mystuff.each do |mr|%></p> > 3: <h1>=mr.m1</h1> > 4: <% end %> > > > HELP :)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: <h1><%= mr.m1 %></h1> HTH Matt
Matt Harrison wrote:> Mark Preston wrote: >>> escape nasties (if appropriate)) >> >> Extracted source (around line #2): >> >> 1: <h1>Mystuff#index</h1> >> 2: <p><%mystuff.each do |mr|%></p> >> 3: <h1>=mr.m1</h1> >> 4: <% end %> >> >> >> HELP :) > > 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: > > <h1><%= mr.m1 %></h1> > > HTH > > MattThanks 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: <h1>Mystuff#index</h1> <p><%me.each do |mr|%></p> <h1><=%mr.m1%></h1> <% end %> and am getting the following error: undefined local variable or method `me'' for #<ActionView::Base:0x381bb24> Extracted source (around line #2): 1: <h1>Mystuff#index</h1> 2: <p><%me.each do |mr|%></p> 3: <h1><=%mr.m1%></h1> 4: <% end %> Thanks for the continued help !!!! -- Posted via http://www.ruby-forum.com/.
On Jul 13, 12:38 am, Mark Preston <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> @my = Mystuff.find( :all)This> <h1>Mystuff#index</h1> > <p><%me.each do |mr|%></p>and this need to match - the instance variables are copied over for you. Fred
Mark Preston wrote:> Matt Harrison wrote: >> Mark Preston wrote: >>>> escape nasties (if appropriate)) >>> >>> Extracted source (around line #2): >>> >>> 1: <h1>Mystuff#index</h1> >>> 2: <p><%mystuff.each do |mr|%></p> >>> 3: <h1>=mr.m1</h1> >>> 4: <% end %> >>> >>> >>> HELP :) >> >> 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: >> >> <h1><%= mr.m1 %></h1> >> >> 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: > > <h1>Mystuff#index</h1> > <p><%me.each do |mr|%></p> > <h1><=%mr.m1%></h1> > <% end %> > > and am getting the following error: > > undefined local variable or method `me'' for > #<ActionView::Base:0x381bb24> > > Extracted source (around line #2): > > 1: <h1>Mystuff#index</h1> > 2: <p><%me.each do |mr|%></p> > 3: <h1><=%mr.m1%></h1> > 4: <% end %> > > > Thanks for the continued help !!!!ok, cleaned up some of my silly errors view looks like this now: <h1>Mystuff#index</h1> <p><%my.each do |mr|%></p> <h1><%=mr.m1%></h1> <% end %> but still getting the error: undefined local variable or method `my'' for #<ActionView::Base:0x381bb24> Extracted source (around line #2): 1: <h1>Mystuff#index</h1> 2: <p><%my.each do |mr|%></p> 3: <h1><%=mr.m1%></h1> 4: <% end %> -- Posted via http://www.ruby-forum.com/.
Frederick Cheung wrote:> On Jul 13, 12:38�am, Mark Preston <rails-mailing-l...@andreas-s.net> > wrote: >> � � � �@my = �Mystuff.find( :all) > This > >> <h1>Mystuff#index</h1> >> <p><%me.each do |mr|%></p> > > and this need to match - the instance variables are copied over for > you. > > FredFred 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: <h1>Mystuff#index</h1> 2: <p><%@my.each do |mr|%></p> 3: <h1><%=mr.m1%></h1> 4: <% end %> and I checked the DB, there is definately data in m1 DB ----- id m1 1 45 2 452 Thanks -- Posted via http://www.ruby-forum.com/.
Mark Preston wrote:> Frederick Cheung wrote: >> On Jul 13, 12:38�am, Mark Preston <rails-mailing-l...@andreas-s.net> >> wrote: >>> � � � �@my = �Mystuff.find( :all) >> This >> >>> <h1>Mystuff#index</h1> >>> <p><%me.each do |mr|%></p> >> >> 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: <h1>Mystuff#index</h1> > 2: <p><%@my.each do |mr|%></p> > 3: <h1><%=mr.m1%></h1> > 4: <% end %> > > and I checked the DB, there is definately data in m1 > > DB > ----- > id m1 > 1 45 > 2 452 > > ThanksI 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 -- Posted via http://www.ruby-forum.com/.
On Jul 13, 12:55 am, Mark Preston <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 >Seems like you were hitting the index action (ie going to /mystuff ) so your read method was never getting called. Fred> Thanks all > > Mark > -- > Posted viahttp://www.ruby-forum.com/.
Mark Preston 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 > > MarkThis 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... -- Posted via http://www.ruby-forum.com/.