Hi, I''m a newbie to rails, so please bear with me. However, I''ve browsed the available documentation, and either didn''t find a solution or it wasn''t evident to my untrained eyes.. I''m working on a portal web application; most if not all pages will have the same layout, with some parts of the page being exactly the same, while others will depend on the context. To this end, I have a layouts/application.rhtml file specifying a three column layout, with the elements (header, navigation menu) which are common to all. I handle the main content with @content_for_layout, driven by whichever controller is responsible for the current page with @content_for_layout. This works fine except it forces me into using a controller for the main page. But more on this later. Finally, I have a right-hand column with context-sensitive boxes. The content needs to depend not only on the controller, but on the current action. This is my main concern: my first take would be to implement this with render_partial ''sidebar'', and inside the sidebar implement what is needed to distinguish between actions. Is this reasonable up to this point or is there a more rails-like way? If it is, what is the best way to get at the current controller/action from inside the partial? Or do I have to explicitely set a variable in the controller to the current action? I''m even more puzzled regading the main content. The home page is in fact a typical aggregator, showing some content from several different controllers. Say I have a ''Post'' controller and a ''Photo'' controller, each with its own list etc actions. Say on the home page I want to list the 5 most popular posts and the 5 most popular pictures. Ideally, I would like not to duplicate code, and instead call upon the same code in the controller (as well as reusing the same partial, which I already do). Hints? Thanks in advance! Bye, Andrea -- Press every key to continue.
> The home page is in fact a > typical aggregator, showing some content from several different > controllers. > Say I have a ''Post'' controller and a ''Photo'' controller, each with its own > list etc actions. Say on the home page I want to list the 5 most popular > posts and the 5 most popular pictures. Ideally, I would like not to > duplicate code, and instead call upon the same code in the controller (as > well as reusing the same partial, which I already do). Hints?You can use components to do this. See http://manuals.rubyonrails.com/read/chapter/72 Regards Simone
On 4/29/05, Simone M. <gal-g/NEdIbV4y0@public.gmane.org> wrote:> > > The home page is in fact a > > typical aggregator, showing some content from several different > > controllers. > > Say I have a ''Post'' controller and a ''Photo'' controller, each with its own > > list etc actions. Say on the home page I want to list the 5 most popular > > posts and the 5 most popular pictures. Ideally, I would like not to > > duplicate code, and instead call upon the same code in the controller (as > > well as reusing the same partial, which I already do). Hints? > > You can use components to do this. > > See http://manuals.rubyonrails.com/read/chapter/72I''ve been trying to use Components in an "Uber-Modular" fashion, but I''m running into all sorts of weird gotchas. For instance, STI leaves out the module when filling out the Type attribute. For instance, if you have Base::Widget, Red::Widget, and Blue::Widget, they all save ''Widget'' as their type, totally messing up STI. -- rick http://techno-weenie.net
Rick Olson
2005-Apr-29 14:52 UTC
Fix for STI when models are in modules... was [Rails] Code organization for portal
> I''ve been trying to use Components in an "Uber-Modular" fashion, but > I''m running into all sorts of weird gotchas. For instance, STI leaves > out the module when filling out the Type attribute. > > For instance, if you have Base::Widget, Red::Widget, and Blue::Widget, > they all save ''Widget'' as their type, totally messing up STI.If anyone runs into this, this is how I solved it without modifying Rails. class Base::Widget < ActiveRecord::Base def ensure_proper_type unless self.class.descends_from_active_record? write_attribute(self.class.inheritance_column, self.class.name[0..12] == ''Controllers::'' ? self.class.name[13..-1] : self.class.name) end end end>From what I gather, Dependencies::LoadingModule loads files in/app/controllers and /components as needed into the Controllers:: namespace. I''m not about to propose a change like this to Rails, so putting this in my base model is fine. -- rick http://techno-weenie.net
> For instance, if you have Base::Widget, Red::Widget, and Blue::Widget, > they all save ''Widget'' as their type, totally messing up STI.That sounds like a bug/design error. I suggest that you file a ticket so that someone who would be better able to make that determination can do so and perhaps do something to remedy this. The whole point of components is this kind of modularity. Of course, as a work around, you could rename Red::Widget to Red::RedWidget and it probably wouldn''t be the end of the world. Brian