Kris Leech
2005-Sep-22 10:51 UTC
Adding Categories to the Depot example in the Agile Rails book
Hi ya, I want to add categories to the depot example in the Agile rails book. These are my thoughts: Create a new table called categories with id, title, description, and image fields. Add a new field to the products table called category_id Create a model called Category with "has_many :products" Now I should have a one to many link between the products and categories tables? Now I need to edit the Store Controller''s index method to display a list of categories instead of products def index @category = Category.list_all end and in the Category model add a list_all method like this: def self.list_all find(:all) end and finally in the Store index view (/store/index.rhtml) <% for category in @categories -%> <img src="<%= category.image_url %>"/> <h3><%= h(category.title) %></h3> <%= category.description %> <% end %> Does this sound correct so far? Thanks for reading this far :) Kris
Tobias Jordans
2005-Sep-22 12:13 UTC
RE: Adding Categories to the Depot example in the Agile Railsbook
Hi, Kris Leech wrote:> Hi ya, I want to add categories to the depot example in the > Agile rails book. (...)have a look at http://www.onlamp.com/lpt/a/5546 The Cookbook-Example described there does exactly what you are looking for. It can be easily adopt (I am doing so right now :)). HTH ~Tobias
Kris Leech
2005-Sep-22 15:00 UTC
Re: Adding Categories to the Depot example in the Agile Railsbook
Thanks for the link that has clarified what I was thinking. But... I have soon run in to a problem - how to separate the customer and admin sides of an app. In the Depot app there is an admin controller with the usual scaffold to edit, destroy, create products. But as soon as you add another table (ie categories) that needs admin functions where do they go? The admin controller already has edit, new, destroy methods for working with the products model. Some ways I thought up: 1.) Prefix all admin methods with the model to which they apply, so: product_edit, product_new, product_destroy, category_edit, category_new, category_destroy. This way you only have to maintain security for one controller and not try and have customer facing methods (list) without security and admin method (new, destroy, edit) with security. 2.) I guess you could get rid of the admin controller and have separate controllers for products and categories with scaffolding but then there would be either no security or customer facing methods like show would ask for a log-in! 3.) Two separate apps one for customer and one for admin. I guess there is a standard way? Thanks again, K. Tobias Jordans wrote:>Hi, > >Kris Leech wrote: > > >>Hi ya, I want to add categories to the depot example in the >>Agile rails book. (...) >> >> > >have a look at >http://www.onlamp.com/lpt/a/5546 >The Cookbook-Example described there does exactly what you are looking for. >It can be easily adopt (I am doing so right now :)). > >HTH >~Tobias > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Interkonect Services UK Ltd. Boundary House Main Street Hoveringham Nottingham NG147 JR web: www.interkonect.com tel: 0115 9663696 fax: 0115 9663696
Alex Young
2005-Sep-23 09:38 UTC
Re: Adding Categories to the Depot example in theAgile Railsbook
Tobias Jordans wrote:> Kris Leech wrote: >>I have soon run in to a problem - how to separate the customer and >>admin sides of an app. >>In the Depot app there is an admin controller with the usual >>scaffold to >>edit, destroy, create products. >>But as soon as you add another table (ie categories) that needs admin >>functions where do they go? >>The admin controller already has edit, new, destroy methods >>for working >>with the products model. > > > I had exactly the same problem and deleted my tryout-webapp three times > until I understood that the book is just ignoring the fact, that the for > excample order-management should be part of /admin/ as well. > > I would be interested in an good solution for this as well!script/generate controller ''admin/product'' script/generate controller ''admin/categories'' script/generate controller ''admin/foobar'' should do the trick.> PS: Who can tell me what exacty "h()" does and why at some places > "#{somecode}" is used (whats it for?)?h() is an alias for html_escape in ERB::Util. http://www.ruby-doc.org/stdlib/erb/rdoc is your friend here. "#{somecode}" is just a way of interpolating the result of a ruby expression into a string variable. -- Alex
Tobias Jordans
2005-Sep-23 10:15 UTC
RE: Adding Categories to the Depot example in theAgile Railsbook
Hi Kris! Kris Leech wrote: Note: This post is more about admin-interface and url-naming than about ist actual topic but I dont wanna change the subject since it''s allready hard enough to find postings that belong together ;).> I have soon run in to a problem - how to separate the customer and > admin sides of an app. > In the Depot app there is an admin controller with the usual > scaffold to > edit, destroy, create products. > But as soon as you add another table (ie categories) that needs admin > functions where do they go? > The admin controller already has edit, new, destroy methods > for working > with the products model.I had exactly the same problem and deleted my tryout-webapp three times until I understood that the book is just ignoring the fact, that the for excample order-management should be part of /admin/ as well. I would be interested in an good solution for this as well!> Some ways I thought up: > > 1.) Prefix all admin methods with the model to which they apply, so: > product_edit, product_new, product_destroy, category_edit, > category_new, > category_destroy. > This way you only have to maintain security for one > controller and not > try and have customer facing methods (list) without security > and admin > method (new, destroy, edit) with security.I thought about this as well. But this would mean some stupid search and replace through the file for some times... And you can use the scaffold-generator only once since otherwise your (changed) file will be overwritten. This doesnt sound like a propper rails-way to do it :).> 2.) I guess you could get rid of the admin controller and > have separate > controllers for products and categories with scaffolding but > then there > would be either no security or customer facing methods like > show would > ask for a log-in!Thats the way I am trying to solve it right now. But I didnt address the problem of login yet. But there sould be a way to say rails that some controllers need a login, some dont... Another thing that i would like to add then is the "/admin/" url-part so make clear that this part of the app belongs to the admin-area. Probably this has to be done with there URL-Rewrite-Methods? Btw: I prefere using the "scaffold :model"-line instead of the scraffold-generator for this since your app isnt packed with the defaultcode...> 3.) Two separate apps one for customer and one for admin.Thats interesting as well! But is this the "railsway"?> I guess there is a standard way?hope someone will tell us ;) ~Tobias PS: Who can tell me what exacty "h()" does and why at some places "#{somecode}" is used (whats it for?)?
James Knight
2005-Sep-23 12:06 UTC
Re: Adding Categories to the Depot example in theAgile Railsbook
Hi, A bit further on in the book (p.126 in the pdf) there is an example of how to call an authorize method as a before_filter, either globally or just within a single class. It also shows an :except parameter which says which methods won''t require login. Is this what you''re looking for? Cheers James Tobias Jordans wrote:> > Thats the way I am trying to solve it right now. > But I didnt address the problem of login yet. > But there sould be a way to say rails that some controllers need a login, > some dont... > Another thing that i would like to add then is the "/admin/" url-part so > make clear that this part of the app belongs to the admin-area. Probably > this has to be done with there URL-Rewrite-Methods? > >
Seemingly Similar Threads
- (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?
- [libnbd PATCH] info: Keep request within 4G bound
- [PATCH nbdinfo v2] info: Add a --map option for displaying allocation metadata.
- [PATCH libnbd] info: Write output atomically.
- balancer://mongrel_cluster