Forum: Ruby on Rails Help with using method_missing to create template path

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1ef0fb9d6b751782e038907dcd5b2d71?d=identicon&s=25 Andy Clarke (Guest)
on 2006-05-19 16:52
(Received via mailing list)
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
39b36b2be47228f8619d61ea7a607a25?d=identicon&s=25 Matthew Beale (Guest)
on 2006-05-19 16:59
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 Beale :: 607 227 0871
Resume & Portfolio @ http://madhatted.com
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-19 17:06
(Received via mailing list)
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
This topic is locked and can not be replied to.