Hi all, I''ve run into a problem with my first rails app. The way that I''m making layouts work is ugly. Since Rails is pretty, I think that I''m doing something wrong. Most of the pages in my site use the a layout that looks something like this: (html) <%= render_partial "toolbar" %> (html) <%= render_partial "navigation" %> (html) <%= render_partial "margin" %> (html) <%= @content_for_layout %> (html) This lets me set up different chunks for the toolbar, navigation, etc for each controller. It''s nice and I like it, but there are a few problems that make it ugly. First, I have to create _toolbar.rhtml, _navigation.rhtml, etc files for every controller or my pages won''t render. Second, many of my pages need to show different partials (such as "margin" and "toolbar") for different actions. I can''t just create a _controllername_margin.rhtml so I have to use a single file and divide it into if blocks that look at url_for. Is there a better way of doing this? Thanks, Casey
You can use a shared directory for the partials in /views/shared. <%= render_partial ''shared/navigation'' %> As for using "_controllername_margin.rhtml," it might be best to put a _margin.rhtml in each view directory instead. -- rick http://techno-weenie.net
Right now, I have: | views +-| category | +-- _margin.rhtml | +-- _navigation.rhtml | +-| user +-- _margin.rhtml +-- _navigation.rhtml But what I (think) I really want is... | views +- _navigation.rhtml (default for all) | +-| category | +-- _margin.rhtml | +-| user +-- _margin.rhtml +-- _navigation.rhtml +-- _navigation_edit.rhtml +-- _navigation_delete.rhtml I hope that my ugly ASCII trees don''t get too mangled by gmail... Casey On Fri, 22 Apr 2005, Rick Olson wrote:> You can use a shared directory for the partials in /views/shared. > > <%= render_partial ''shared/navigation'' %> > > As for using "_controllername_margin.rhtml," it might be best to put a > _margin.rhtml in each view directory instead. > > -- > rick > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This is why layouts exist [1]. Put in your application controller "layout ''main''". Then in "./views/layouts" you have a main.rhtml that defines the main structure. Inside this file you can render various partials from the layouts directory. Then add a <%= @content_for_layout %> inside main.rhtml to render all the stuff from the controller specific view. I can provide more details if you need later today, right now my lunch is over... Jason [1] http://rails.rubyonrails.com/classes/ActionController/Layout/ClassMethods.html
On Friday 22 April 2005 12:34, Casey wrote:> Most of the pages in my site use the a layout that looks something like > this: > > (html) > <%= render_partial "toolbar" %>You can also do <%= @toolbar_html || render_partial(...) %> Then in your ApplicationController, define def without_toolbar() @toolbar_html = "" end def with_toolbar(some options) @toolbar = some html generated by your choice of means end Then do before_filter :without_toolbar to disable the toolbar... -- Nicholas Seckar aka. Ulysses