Hi guys, I''m new to Rails, as a starting project I''m trying to put together a showcase site for some of my projects (web design, audio production etc...). I''d like to build a basic CMS so I can quickly add updates, and also to practice my Rails skills. I''ve been reading the ''Agile Web Development with Rails'' book which is great and I''ve been building off the examples in there. I''m thinking that the way to go is to have a model for the web stuff (web_item) and a model for the audio stuff (audio_item) and the associated databases for each. I then generated an admin controller, then generated a scaffold for the web_item in the admin controller. This all works ok and I can add data to the database and see my changes in the browser. But this is obviously only giving me access to the web_item model. I can''t think of a way to give me access to the audio_item model as well, using only the admin controller. Hope that makes sense, sorry if it''s going to be really obvious. Basically I want an admin section, which I can then choose whether to edit the web stuff or the audio stuff, then have an area for each. Thanks, Henry Henry Bourne: Music Production - Audio Engineering - Web Development - Session Drumming Contact: www.henrybourne.co.uk - henry.bourne@gmail.com - 07740 192427 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060725/f944dbac/attachment.html
Hi Henry, Well there are lots of ways to do it. One is to forget about admin_controller all together. Especially if you will be the only admin of your site. I think you would be best off simply having your web_items_controller and your audio_items_controller. then you have your Create-Update-Read-Distroy actions in them. Then you can use a before_filter in the controllers to make sure only you can do anything but read: before_filter :protect_from_public :except => :index, :list, :show Then define protect_from_public somewhere (maybe application.rb is a good start): def protect_from_public unless session[:admin_user] redirect_to( :controller => ''accounts'', :action => :login) and return false end end then you would have an account_controller to handle your login and logout. AWD had some stuff in it that would give you a good head start with that too. anyway thats the idea. Hope that helps. Henry Bourne wrote:> Hi guys, > > I''m new to Rails, as a starting project I''m trying to put together a > showcase site for some of my projects (web design, audio production > etc...). I''d like to build a basic CMS so I can quickly add updates, > and also to practice my Rails skills. > > I''ve been reading the ''Agile Web Development with Rails'' book which > is great and I''ve been building off the examples in there. I''m > thinking that the way to go is to have a model for the web stuff > (web_item) and a model for the audio stuff (audio_item) and the > associated databases for each. I then generated an admin controller, > then generated a scaffold for the web_item in the admin controller. > > This all works ok and I can add data to the database and see my > changes in the browser. But this is obviously only giving me access > to the web_item model. I can''t think of a way to give me access to > the audio_item model as well, using only the admin controller. > > Hope that makes sense, sorry if it''s going to be really obvious. > Basically I want an admin section, which I can then choose whether to > edit the web stuff or the audio stuff, then have an area for each. > > Thanks, > Henry > > > Henry Bourne: Music Production - Audio Engineering - Web Development > - Session Drumming > Contact: www.henrybourne.co.uk - henry.bourne@gmail.com - 07740 192427-- Posted via http://www.ruby-forum.com/.
That does make a lot more sense than the way I was trying! Thanks very much, I''ll give it a bash and see how I get on. :) Henry Henry Bourne: Music Production - Audio Engineering - Web Development - Session Drumming Contact: www.henrybourne.co.uk - henry.bourne@gmail.com - 07740 192427 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060725/d10d2c4f/attachment.html
Hey Henry, If your still there. I just wanted to say ''that is only one way of doing it'' one more time. To be honest its probably not the most common way that I have seen in other projects but its my approach. Probably a more common way would be to have an admin directory in your controllers. So you would have a audio_items_controller and then you would have an admin/audio_items_controller. That way you could really easily go to www.yoursite/admin and you would be at your admin interface and cruse around in there going to different controllers and just leave the controllers outside of that directory to mostly do public display stuff. there is also more to that approach, but its pretty simple and how I started out. It might be easier to deal with. And there are good examples of that kind of structure being used all over. Check out just about any OS rails app and look into the controller directory and you will see an admin folder with repeating controllers that have been made for editing and stuff in it, while the top level controllers do mostly display stuff. I got worried there because I didn''t want to give you the impression that the way I described was what most of the rails apps are doing today. Its the way I find most sensible and use in my own work. If I had been less hypnotized by my own genius ;) when I wrote my first response I would have probably told you to make an admin module like I mentioned above. The best way to get some ideas is by looking at how other guys are doing it. I would suggest looking around in mephisto or typo. You''ll learn alot. best wishes, john -- Posted via http://www.ruby-forum.com/.
Spot on! Thanks very much! That''s defintely cleared things up for me. I''ve just been having a play around, and everything seems to be coming together now! I''ll defnintely be coming back here if I have any other problems in the future. Thanks again! :) Henry -- Posted via http://www.ruby-forum.com/.
Henry Bourne wrote:> I guess what I''m trying to say is, can I have any number of controllers > dealing with a certain model? So in the example of my site again, can I > have a controller called ''web'' which can get data from the ''web_items'' > model and display it using the ''web'' view which would be what the public > sees. Then also like you suggested an admin controller folder containing > controllers which can access the same model but use different views and > actions and require authorisation to log in? And could I generate a > scaffold from each of these to help me get started? > > Thanks > HenryThats exactly right. You can call your controllers whatever you want and have as many as you want too. There is nothing that really binds a controller to a model. It does tend to happen though, that you have controllers and models with similar concepts in mind. So you usually do have your WebItem model (whos job is to take care of its state and provide class methods for finding WebItems in the DB and stuff) and then a WebItemsController (who''s job is to control or direct or delegate the flow of information between the WebItem models and the WebItem views based on the request that comes in through the server). But this is just a convenient pattern for organizing things. It just tends to keep things clear. But it is in no way necessary. You can be as creative as you would like. To be honest if you really wanted to have a single admin_controller be the one controller for all of your models it would work fine. There is nothing stopping you from filling up a controller with every action under the sun. So you could have made actions like edit_audio_item and edit_web_item and basically crammed two scaffolding like sets of methods into the same controller (each working on either web_items or audio_items). Then in the future, any time you wanted to add a new model that needed to be edited you could shoehorn another set of actions in there. But this is clearly not a good way to keep things in good order. But anyway yeah, controllers aren''t tightly bound to models like they are to views or anything. I hope that helps (or at least makes sense). -- Posted via http://www.ruby-forum.com/.
Thanks very much for your help! What you just described is the kind of thing I was trying to do in the first place, but I couldn''t quite get my head around doing it. I still have a few related issues with rails in general that are throwing me a bit and would greatly appreciate your advice if that''s ok? In the example of my site, I make a model ''web_item'' which also creates a database ''web_items'' and has the class name ''WebItem''. Now at the moment, I''m kind of thinking that I also have to make a controller called ''web_items''. What''s throwing me is the depot example in the AWD book that just has the one main ''Products'' database and a ''products'' controller thats deals with that. I guess what I''m trying to say is, can I have any number of controllers dealing with a certain model? So in the example of my site again, can I have a controller called ''web'' which can get data from the ''web_items'' model and display it using the ''web'' view which would be what the public sees. Then also like you suggested an admin controller folder containing controllers which can access the same model but use different views and actions and require authorisation to log in? And could I generate a scaffold from each of these to help me get started? Thanks Henry john wrote:> Hey Henry, > If your still there. I just wanted to say ''that is only one way of > doing it'' one more time. To be honest its probably not the most common > way that I have seen in other projects but its my approach. > > Probably a more common way would be to have an admin directory in your > controllers. So you would have a audio_items_controller and then you > would have an admin/audio_items_controller. That way you could really > easily go to www.yoursite/admin and you would be at your admin interface > and cruse around in there going to different controllers and just leave > the controllers outside of that directory to mostly do public display > stuff. > > there is also more to that approach, but its pretty simple and how I > started out. It might be easier to deal with. And there are good > examples of that kind of structure being used all over. Check out just > about any OS rails app and look into the controller directory and you > will see an admin folder with repeating controllers that have been made > for editing and stuff in it, while the top level controllers do mostly > display stuff. > > I got worried there because I didn''t want to give you the impression > that the way I described was what most of the rails apps are doing > today. Its the way I find most sensible and use in my own work. If I > had been less hypnotized by my own genius ;) when I wrote my first > response I would have probably told you to make an admin module like I > mentioned above. > > The best way to get some ideas is by looking at how other guys are doing > it. I would suggest looking around in mephisto or typo. You''ll learn > alot. > > best wishes, > john-- Posted via http://www.ruby-forum.com/.