Can anyone offer help on how I''d list fields in a database that are related to a category(in another table? This would be an example of what''s displayed. Category field1 field2 field3 field4 field5 -- Posted via http://www.ruby-forum.com/.
On 15.11.2005, at 23.57, Guest wrote:> Can anyone offer help on how I''d list fields in a database that are > related to a category(in another table?in controller: @cat = Category.find(params[:id], :include => :fields) then in view: <%= @cat.title %> <ul> <% for field in @cat.fields %> <li><%= field.title %></li> <% end %> </ul> I assume you have "has_many :fields" in your Category class definition already. //jarkko> > This would be an example of what''s displayed. > > > > Category > > field1 > field2 > field3 > field4 > field5 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org
2005-Nov-16 15:41 UTC
Re: Category and List from a database
jarkko wrote:> On 15.11.2005, at 23.57, Guest wrote: > >> Can anyone offer help on how I''d list fields in a database that are >> related to a category(in another table? > > in controller: > @cat = Category.find(params[:id], :include => :fields) > > then in view: > <%= @cat.title %> > <ul> > <% for field in @cat.fields %> > <li><%= field.title %></li> > <% end %> > </ul> > > I assume you have "has_many :fields" in your Category class > definition already. > > //jarkkoAre you familiar with the ONLAMP tutorial "Rolling with Ruby on Rails"? I''d like to be able to just list out the recipes under the Categories on one page....instead of having to click a category link and have it list on a seperate one. Have any idea how that would be done? Thanks for your help. -- Posted via http://www.ruby-forum.com/.
On 16.11.2005, at 17.41, hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org wrote:> Are you familiar with the ONLAMP tutorial "Rolling with Ruby on > Rails"? > I''d like to be able to just list out the recipes under the > Categories on > one page....instead of having to click a category link and have it > list > on a seperate one. > > Have any idea how that would be done?Not much different. Something like this: in controller: @cats = Category.find(:all, :include => :fields) # :all will make this return an array of cats then in view: <% for cat in @cats %> <h3><%= cat.title %></h3> <ul> <% for field in cat.fields %> <li><%= field.title %></li> <% end %> </ul> <% end %> These are pretty fundamental Ruby/Rails things, tho. You might want to buy the Agile Web Development with Rails book [1]. It is invaluable if you plan to use Rails at all. //jarkko [1] http://www.pragmaticprogrammer.com/titles/rails/index.html -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
<hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
2005-Nov-16 16:11 UTC
Re: Re: Category and List from a database
jarkko wrote:> On 16.11.2005, at 17.41, hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org wrote: >> Are you familiar with the ONLAMP tutorial "Rolling with Ruby on >> Rails"? >> I''d like to be able to just list out the recipes under the >> Categories on >> one page....instead of having to click a category link and have it >> list >> on a seperate one. >> >> Have any idea how that would be done? > > Not much different. Something like this: > > in controller: > @cats = Category.find(:all, :include => :fields) # :all will make > this return an array of cats > > then in view: > <% for cat in @cats %> > <h3><%= cat.title %></h3> > <ul> > <% for field in cat.fields %> > <li><%= field.title %></li> > <% end %> > </ul> > <% end %> > > These are pretty fundamental Ruby/Rails things, tho. You might want > to buy the Agile Web Development with Rails book [1]. It is > invaluable if you plan to use Rails at all. > > //jarkko > > [1] http://www.pragmaticprogrammer.com/titles/rails/index.htmlI am reading the book now actually, I''m just trying to learn some by figuring out some solutions. When you write "fields", would that be the same as "recipes" from the ONLamp tutorial? -- Posted via http://www.ruby-forum.com/.
<hatejonny5-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
2005-Nov-16 16:19 UTC
Re: Re: Category and List from a database
And, would that be written in the Category controller or Recipe? I''m sorry for the really basic questions, but I''m very very new to the game. Thanks. -- Posted via http://www.ruby-forum.com/.
On 16.11.2005, at 18.11, <hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> <hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > I am reading the book now actually, I''m just trying to learn some by > figuring out some solutions. > > When you write "fields", would that be the same as "recipes" from the > ONLamp tutorial?It can be whatever you want. The point is that there is a one-to-many relationship between category and fields/recipes. In Rails lingo, category has_many :recipes. On 16.11.2005, at 18.19, <hatejonny5-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> <hatejonny5-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> And, would that be written in the Category controller or Recipe? > I''m sorry for the really basic questions, but I''m very very new to the > game. > > Thanks.That would be written in the controller/view where you want to show that list. Categories and Recipes are models in your application, they''re not directly connected to any controller. So you can divide your app up in controllers in a way that makes the most sense to you. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Huge thanks Jarkko. I was able to implement what you wrote and get exactly what I was looking for. Any other materials you''d recommend reading/doing to get better at Ruby/Rails? --- Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> On 16.11.2005, at 18.11, <hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > <hatejonny-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > wrote: > > > > I am reading the book now actually, I''m just > trying to learn some by > > figuring out some solutions. > > > > When you write "fields", would that be the same as > "recipes" from the > > ONLamp tutorial? > > It can be whatever you want. The point is that there > is a one-to-many > relationship between category and fields/recipes. In > Rails lingo, > category has_many :recipes. > > On 16.11.2005, at 18.19, <hatejonny5-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > <hatejonny5-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > And, would that be written in the Category > controller or Recipe? > > I''m sorry for the really basic questions, but I''m > very very new to the > > game. > > > > Thanks. > > That would be written in the controller/view where > you want to show > that list. Categories and Recipes are models in your > application, > they''re not directly connected to any controller. So > you can divide > your app up in controllers in a way that makes the > most sense to you. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
On 16.11.2005, at 19.08, jonathan Mcintire wrote:> Huge thanks Jarkko.My pleasure.> I was able to implement what you wrote and get exactly > what I was looking for. > Any other materials you''d recommend reading/doing to > get better at Ruby/Rails?After you''ve perused the Rails book and can honestly say that you''ve digested it all, you''ve gone a long way. Anyway, you probably learn best by doing, so working through the tutorial in the book will probably teach you more than enough to get rolling. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails