Hi, I''m not really a developer but I''ve spent the past week or two learning about Rails, as I believe it would give me enough of a leg-up to be able to create some useful web apps. Before I begin, I''ll just say that I''ve spent almost all weekend reading up on this and trying to understand how this works. I''m sure it''s very simple but it''s proving to be a major stumbling block. Sorry if I''m repeating what has been asked before but I couldn''t find an answer either in this list''s archives, the Agile Web Development book or elsewhere. I''m having great difficulty getting my head around the way joins work in Rails. For example, I have a products table and a sub_categories table. The products table has an integer column of sub_category_id. In the Products model I have: belongs_to :sub_category and in the SubCategory model I have: has_many :products I''ve ammended the scaffold-generated views so that the list looks like: <table> <tr> <th>Name</th> <th>Price</th> <th>Short description</th> <th>Sub category</th> <th>Live?</th> </tr> <% for product in @products %> <tr> <td><%=h product.name %></td> <td><%=h product.price_id %></td> <td><%=h product.short_desc %></td> <td><%=h puts product.sub_category.name %></td> <td><%=h sprintf("Yes") if product.live == 1 %></td> <td><%= link_to ''Show'', :action => ''show'', :id => product %></td> <td><%= link_to ''Edit'', :action => ''edit'', :id => product %></td> <td><%= link_to ''Destroy'', {:action => ''destroy'', :id => product}, :confirm => ''Are you sure?'' %></td> </tr> <% end %> </table> Now, the <%=h puts product.sub_category.name %> bit of ERb outputs a blank, so the HTML it renders is simply <td></td>. I''m sure that I''m missing something really obvious here :) Do I need to load the appropriate category name into a variable in the model class? I''m pretty sure that''s not it, because I thought Rails handled that kinda of thing automatically. Anyway, if anyone can help I''ll be most grateful :) I''m pretty certain that once I get my head round this I''ll have a much better understanding of Rails. Cheers! -- Matthew Revell www.understated.co.uk
drop the "puts" <%=h puts product.sub_category.name %> should be: <%=h product.sub_category.name %> what does your underlying data in the database look like? Matthew Revell wrote:>Hi, > >I''m not really a developer but I''ve spent the past week or two >learning about Rails, as I believe it would give me enough of a leg-up >to be able to create some useful web apps. > >Before I begin, I''ll just say that I''ve spent almost all weekend >reading up on this and trying to understand how this works. I''m sure >it''s very simple but it''s proving to be a major stumbling block. Sorry >if I''m repeating what has been asked before but I couldn''t find an >answer either in this list''s archives, the Agile Web Development book >or elsewhere. > >I''m having great difficulty getting my head around the way joins work >in Rails. For example, I have a products table and a sub_categories >table. The products table has an integer column of sub_category_id. > >In the Products model I have: > >belongs_to :sub_category > >and in the SubCategory model I have: > >has_many :products > >I''ve ammended the scaffold-generated views so that the list looks like: > ><table> > <tr> > <th>Name</th> > <th>Price</th> > <th>Short description</th> > <th>Sub category</th> > <th>Live?</th> > </tr> > ><% for product in @products %> > <tr> > <td><%=h product.name %></td> > <td><%=h product.price_id %></td> > <td><%=h product.short_desc %></td> > <td><%=h puts product.sub_category.name %></td> > <td><%=h sprintf("Yes") if product.live == 1 %></td> > <td><%= link_to ''Show'', :action => ''show'', :id => product %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => product %></td> > <td><%= link_to ''Destroy'', {:action => ''destroy'', :id => product}, >:confirm => ''Are you sure?'' %></td> > </tr> ><% end %> ></table> > >Now, the <%=h puts product.sub_category.name %> bit of ERb outputs a >blank, so the HTML it renders is simply <td></td>. > >I''m sure that I''m missing something really obvious here :) Do I need >to load the appropriate category name into a variable in the model >class? I''m pretty sure that''s not it, because I thought Rails handled >that kinda of thing automatically. > >Anyway, if anyone can help I''ll be most grateful :) I''m pretty certain >that once I get my head round this I''ll have a much better >understanding of Rails. > >Cheers! > > >
puts outputs data to stdout not to the response -- look in your WEBrick console and you might see the output you''re expecting. puts also returns no value, which is then passed to h(), which results in "", which is sent in the response. Try removing puts: <td><%=h product.sub_category.name %></td> There is also a concat function which behaves like PHP''s echo or ASP''s response.write. If you want to use this method (notice the tag is now <% instead of <%=. This case is kind of trivial, but there are other cases where it might make sense to use concat): <td><%concat h(product.sub_category.name) %></td> On 9/12/05, Matthew Revell <matthew.revell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I''m not really a developer but I''ve spent the past week or two > learning about Rails, as I believe it would give me enough of a leg-up > to be able to create some useful web apps. > > Before I begin, I''ll just say that I''ve spent almost all weekend > reading up on this and trying to understand how this works. I''m sure > it''s very simple but it''s proving to be a major stumbling block. Sorry > if I''m repeating what has been asked before but I couldn''t find an > answer either in this list''s archives, the Agile Web Development book > or elsewhere. > > I''m having great difficulty getting my head around the way joins work > in Rails. For example, I have a products table and a sub_categories > table. The products table has an integer column of sub_category_id. > > In the Products model I have: > > belongs_to :sub_category > > and in the SubCategory model I have: > > has_many :products > > I''ve ammended the scaffold-generated views so that the list looks like: > > <table> > <tr> > <th>Name</th> > <th>Price</th> > <th>Short description</th> > <th>Sub category</th> > <th>Live?</th> > </tr> > > <% for product in @products %> > <tr> > <td><%=h product.name <http://product.name> %></td> > <td><%=h product.price_id %></td> > <td><%=h product.short_desc %></td> > <td><%=h puts product.sub_category.name %></td> > <td><%=h sprintf("Yes") if product.live == 1 %></td> > <td><%= link_to ''Show'', :action => ''show'', :id => product %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => product %></td> > <td><%= link_to ''Destroy'', {:action => ''destroy'', :id => product}, > :confirm => ''Are you sure?'' %></td> > </tr> > <% end %> > </table> > > Now, the <%=h puts product.sub_category.name %> bit of ERb outputs a > blank, so the HTML it renders is simply <td></td>. > > I''m sure that I''m missing something really obvious here :) Do I need > to load the appropriate category name into a variable in the model > class? I''m pretty sure that''s not it, because I thought Rails handled > that kinda of thing automatically. > > Anyway, if anyone can help I''ll be most grateful :) I''m pretty certain > that once I get my head round this I''ll have a much better > understanding of Rails. > > Cheers! > > -- > Matthew Revell > www.understated.co.uk <http://www.understated.co.uk> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver brockweaver-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org /* you are not expected to understand this */ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/09/05, Francois Paul <francois-VKbYeNyhmt9BDgjK7y7TUQ@public.gmane.org> wrote:> drop the "puts" > > <%=h puts product.sub_category.name %> > > should be: > > <%=h product.sub_category.name %> >Thanks both Francois and Brock. I was pretty sure that I''d done that previously, but I was getting an "unknown method" error. There must have been some other problem which I unwittingly fixed.