baker1
2006-Aug-10 13:07 UTC
[Rails] passing hash from controller to view and pluralization?
hi, i have 2 tables (counties and towns). counties has_many towns and towns belong_to counties. now my question: i thought i would need to do is say @counties = Counties.find(:all). should that not return to me all counties in the counties table WITH all towns associated with each county? in my view i was getting error when doing this if(counties.has_towns?) saying undefined has_towns methods... another question about plurulization. if i were to say @county vs @counties, is there a difference? would RoR assume i just want information for 1 county? or does RoR treat it like the same variable? -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Aug-10 21:19 UTC
[Rails] Re: passing hash from controller to view and pluralization?
Also, your controller should be @counties = Counties.find(:all) Then your view should have <% @counties.each do |county| %> <% if county.towns.any? %> <%= link_to county.name, :action => ''show'', :id => county %><br /> <% end %> <% end %> Or something similar. Note that in your example you cant call any model specific methods on @counties because it is an array of County objects, not a county object itself. -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Aug-10 21:19 UTC
[Rails] Re: passing hash from controller to view and pluralization?
baker1 wrote:> in my view i was getting error when doing this > > if(counties.has_towns?) > > saying undefined has_towns methods...If you have "has_many :towns" in your model definition, than "towns" is the methods you use to access the association. @county.towns #=> an array of Town objects If you want to know if this county has any associated towns, remember that it returns an array. So you can use any array methods on it. if @county.towns.any? do_stuff end> another question about plurulization. if i were to say @county vs > @counties, is there a difference? would RoR assume i just want > information for 1 county? or does RoR treat it like the same variable?It''s entirely up to you. Rails doesn''t care what you name your variables. def index @foo = Something.find(1) @bar = Domething.find(:all) end Now from index.rhtml @foo is a single Something object, and @bar is an array of Something objects. However convention tell us it''s best to names arrays of things plural and single object singular. -- Posted via http://www.ruby-forum.com/.
baker1
2006-Aug-10 21:22 UTC
[Rails] Re: passing hash from controller to view and pluralization?
hello alex, i really appreciate your help! if @county.towns contains the towns for the particular county, how can i now increment to the next county and show the next set of towns? basically id like to display something like this for county in @counties print county name for towns in @counties print town name end end is this possible or do i need more than one data container passed to the view? Alex Wayne wrote:> baker1 wrote: >> in my view i was getting error when doing this >> >> if(counties.has_towns?) >> >> saying undefined has_towns methods... > > If you have "has_many :towns" in your model definition, than "towns" is > the methods you use to access the association. > > @county.towns #=> an array of Town objects > > If you want to know if this county has any associated towns, remember > that it returns an array. So you can use any array methods on it. > > if @county.towns.any? > do_stuff > end > >> another question about plurulization. if i were to say @county vs >> @counties, is there a difference? would RoR assume i just want >> information for 1 county? or does RoR treat it like the same variable? > > It''s entirely up to you. Rails doesn''t care what you name your > variables. > > def index > @foo = Something.find(1) > @bar = Domething.find(:all) > end > > Now from index.rhtml @foo is a single Something object, and @bar is an > array of Something objects. However convention tell us it''s best to > names arrays of things plural and single object singular.-- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Aug-10 22:24 UTC
[Rails] Re: passing hash from controller to view and pluralization?
baker1 wrote:> > hello alex, i really appreciate your help! > > > if @county.towns contains the towns for the particular county, how can i > now increment to the next county and show the next set of towns? > > > basically id like to display something like this > > for county in @counties > print county name > > for towns in @counties > print town name > end > end > > is this possible or do i need more than one data container passed to the > view?you almost have it <% for county in @counties %> <h1><%= county.name %></h1> <% for town in county.towns %> <p><%= town.name %></p> <% end %> <% end %> -- Posted via http://www.ruby-forum.com/.
baker1
2006-Aug-11 10:48 UTC
[Rails] Re: passing hash from controller to view and pluralization?
hey Alex thanks, i will try this when i get home today. coming from a c++ perspective, i am still having difficulty understanding how ruby and rails work. its like what are big no-no''s in c++ is a.o.k. in ruby! a few more questions if you dont mind: now @counties is the object i declared in my controller that contains everything that my mySql counties table has? what is singular ''county'' in the for loop? is that the same as saying for(int i = 0; i < number of counties in counties table; i++) { do stuff to each county etc...? } Alex Wayne wrote:> baker1 wrote: >> >> hello alex, i really appreciate your help! >> >> >> if @county.towns contains the towns for the particular county, how can i >> now increment to the next county and show the next set of towns? >> >> >> basically id like to display something like this >> >> for county in @counties >> print county name >> >> for towns in @counties >> print town name >> end >> end >> >> is this possible or do i need more than one data container passed to the >> view? > > you almost have it > > <% for county in @counties %> > <h1><%= county.name %></h1> > <% for town in county.towns %> > <p><%= town.name %></p> > <% end %> > <% end %>-- Posted via http://www.ruby-forum.com/.
Romeu Henrique Capparelli Fonseca
2006-Aug-11 12:09 UTC
RES: [Rails] Re: passing hash from controller to view and pluralization?
Hi, Assuming that in your view the only hash you receive is @counties we can iterate its members this way: for county in @counties print county.name for town in county.towns print town.name end end --Romeu -----Mensagem original----- De: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] Em nome de baker1 Enviada em: quinta-feira, 10 de agosto de 2006 17:22 Para: rails@lists.rubyonrails.org Assunto: [Rails] Re: passing hash from controller to view and pluralization? hello alex, i really appreciate your help! if @county.towns contains the towns for the particular county, how can i now increment to the next county and show the next set of towns? basically id like to display something like this for county in @counties print county name for towns in @counties print town name end end is this possible or do i need more than one data container passed to the view? Alex Wayne wrote:> baker1 wrote: >> in my view i was getting error when doing this >> >> if(counties.has_towns?) >> >> saying undefined has_towns methods... > > If you have "has_many :towns" in your model definition, than "towns" is > the methods you use to access the association. > > @county.towns #=> an array of Town objects > > If you want to know if this county has any associated towns, remember > that it returns an array. So you can use any array methods on it. > > if @county.towns.any? > do_stuff > end > >> another question about plurulization. if i were to say @county vs >> @counties, is there a difference? would RoR assume i just want >> information for 1 county? or does RoR treat it like the same variable? > > It''s entirely up to you. Rails doesn''t care what you name your > variables. > > def index > @foo = Something.find(1) > @bar = Domething.find(:all) > end > > Now from index.rhtml @foo is a single Something object, and @bar is an > array of Something objects. However convention tell us it''s best to > names arrays of things plural and single object singular.-- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.1.405 / Virus Database: 268.10.8/415 - Release Date: 9/8/2006 -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.405 / Virus Database: 268.10.9/416 - Release Date: 10/8/2006