Andy Clarke
2006-May-19 14:51 UTC
[Rails] Help with using method_missing to create template path
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
Matthew Beale
2006-May-19 14:59 UTC
[Rails] Re: Help with using method_missing to create template path
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 -- Posted via http://www.ruby-forum.com/.
Brian Hughes
2006-May-19 15:03 UTC
[Rails] Help with using method_missing to create template path
On May 19, 2006, at 10:52 AM, Andy Clarke wrote:> 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.)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