Help with using method_missing to create template path


#1

I have a site (for an academic conference) that I want to convert to RoR

  • both as a learning exercise and to ease further updates and
    development. There are a number of child pages for each conference (past
    and future), but these pages are different for each conference (not same
    child pages for each conference). The URLs are along the lines of:

    /conference/2000/keynotes
    /conference/2003/papers

Is it possible to have a method_missing in my ConferenceController that
will take the “2003” and “papers” bits of the URL and display
“/views/conference/2003/papers.rhtml”? I have added the following to my
routes.rb

map.connect 'conference/:year/:action', :controller => 'conference'

but am having difficulty in writing a method_missing that will work -
taking the year part of the URL and using this with the action to create
the right template path. What I am trying to do is keep
ConferenceController simple and avoid having to add a method to it every
time I add a different type of child page (keynotes, accommodation,
papers, etc.)

Any help or advice would be appreciated, as would any suggestions for an
alternative/better approach.

Thanks

Andy


#2

Andy Clarke wrote:

I have a site (for an academic conference) that I want to convert to RoR

  • both as a learning exercise and to ease further updates and
    development. There are a number of child pages for each conference (past
    and future), but these pages are different for each conference (not same
    child pages for each conference). The URLs are along the lines of:

    /conference/2000/keynotes
    /conference/2003/papers

Is it possible to have a method_missing in my ConferenceController that
will take the “2003” and “papers” bits of the URL and display
“/views/conference/2003/papers.rhtml”? I have added the following to my
routes.rb

map.connect 'conference/:year/:action', :controller => 'conference'

but am having difficulty in writing a method_missing that will work -
taking the year part of the URL and using this with the action to create
the right template path. What I am trying to do is keep
ConferenceController simple and avoid having to add a method to it every
time I add a different type of child page (keynotes, accommodation,
papers, etc.)

You don’t need a method_missing there. Don’t you know all the action’s
you’ll need to create? Keynotes, Papers, etc would just become your
methods in the conference controller.


Matthew B. :: 607 227 0871
Resume & Portfolio @ http://madhatted.com


#3

On May 19, 2006, at 10:52 AM, Andy Clarke wrote:

map.connect ‘conference/:year/:action’, :controller => ‘conference’

but am having difficulty in writing a method_missing that will work

  • taking the year part of the URL and using this with the action to
    create the right template path. What I am trying to do is keep
    ConferenceController simple and avoid having to add a method to it
    every time I add a different type of child page (keynotes,
    accommodation, papers, etc.)

I don’t think method_missing is actually want you want to use, but
you definitely want to mimic that kind of catch-all functionality.
Here’s how I have done this in the past:

First, we change the route to be more like this:

map.connect ‘conference/:year/:page’, :controller =>
‘conference’, :action => ‘show_page’

Then, in your Conference controller, you have a show_page action.
That action gets the data from params[:year] and params[:page] to
determine what template you want to display. Something along these
lines:

def show_page
year = params[:year]
page = params[:page]
render :template => “conference/#{year}/#{page}”
end

-Brian