0

Currently we are using method_missing to catch for calls to SEO friendly actions in our controllers rather than creating actions for every conceivable value for a variable. What we want are URLS like this:

/students/BobSmith

and NOT /students/show/342

IS there a cleaner solution than method_missing?

Thank you!

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
tesserakt
  • 3,231
  • 4
  • 27
  • 40

2 Answers2

0

You can create a catch-all route. Put this at the bottom of config/routes.rb with whatever controller and action you want:

map.connect '*path', :controller => '...', :action => '...'

The segments of the route will be available to your controller in the params[:path] array.

John Topley
  • 113,588
  • 46
  • 195
  • 237
0

You can define a route for that particular format fairly easily.

map.connect "/students/:name", :controller => :students, :action => :show, :requirements => {:name => /[A-Z][A-Z]+/}

Then in your show action you can find by name using params[:name].

Alan Peabody
  • 3,507
  • 1
  • 22
  • 26
  • This only works if the the names are unique. If not, then you should prefix the name with the Student's ID. Something like "/students/342-Bob-Smith" and map.connect "/students/:id". Then, just do params[:id].to_i to get the student's ID. – Michael Melanson Jun 15 '10 at 20:19
  • This seems like it would be close to what I want. In essence, this would move the current login into a different method, and then leave method_missing open for when there really was a method missing. – tesserakt Jun 16 '10 at 03:27
  • Yep, keep in mind that they do have to be unique as Michael mentioned, but I really can not imagine that there is anything you can not do with the routes file and have to use method missing. You may want to look up ActiveRecord::Base.to_param method and how to create seo friendly urls with that. – Alan Peabody Jun 16 '10 at 05:20