开发者

How can I route to different controllers with the same URL format in Rails?

I have two different models that can show up in a category. In my routes.rb, I would like to have something like this:

ActionController::Routing::Routes.draw do |map|
  map.top_list ':category/:foo', :controller => 'foo', :action => 'show'
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end

This works fine when I load a URL like "/some-category-name/some-foo-name", where "some-foo-name" c开发者_C百科an be loaded by the FooController like so:

class FooController < ApplicationController
  def show
    @foo = Foo.find_by_url! params[:foo]
  end
end

But when I try to request a Bar, like "/some-category-name/some-bar-name", I get a "ActiveRecord::RecordNotFound in FooController#show". I know that I can solve this problem by requiring that all Foo names start with "foo-" and all Bar names start with "bar-", then defining routes like this:

ActionController::Routing::Routes.draw do |map|
  map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => /^foo-/ }
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show', :requirements => { :name => /^bar-/ }
end

But forcing this restriction on names is quite suboptimal. I found the following, which looks like it might work for me: Different routes but using the same controller for model subclasses in Rails. However, I don't quite follow the example, so I don't know if this would solve my problem. It is also not great that my Foo and BarController would have to inherit from CategoryController.

One thought that occurred to me is that I could try to look up the Foo, then fall back to the BarController if that fails. I can easily do this in the FooController and redirect to the BarController, but this is not really OK, since all the requests for Bars will be logged as FooController#show in the Rails log. However, if I could somehow configure the routes to call a method to determine what to route to based on the basename of the URL, I could get the behaviour that I need; e.g.

ActionController::Routing::Routes.draw do |map|
  is_a_foo = Proc.new {|name| Foo.find_by_url! name && true }

  map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => is_a_foo }
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end

Is there any way to do this in bog-standard Rails 2?


I ended up writing a little Rails plugin that allows me to write a route like this:

map.with_options :category => /[-A-Za-z0-9]+/ do |m|
  # Since both foos and bars can appear under category, we define a route for foo that only matches when
  # the foo can be found in the database, then fall back to the bar route
  m.foo ':category/:foo', :controller => 'foo', :action => 'show', :conditions => {
      :url => { :find_by_url => Foo }
  }
  m.bar ':category/:bar', :controller => 'bar', :action => 'show'
end

Until I get permission to open source the code, I have to leave the implementation as an exercise for the reader, but here's how I got there:

  1. Monkey-patching Rails: Extending Routes #2
  2. Under the hood: route recognition in Rails
  3. The Complete Guide to Rails Plugins: Part II

I'll update this answer with a github link when I can. :)


This seems wrong since you are using the same route to two controllers. Rails will choose one only as you noticed.

It would be nicer to use something like this:

map.top_list ':category/foo/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => is_a_foo }
map.top_list ':category/bar/:bar', :controller => 'bar', :action => 'show'

Since in provided routes you give two params each time. It does not matter that you name them differently. It just matches both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜