我在我的 rails 应用程序中使用 ActiveResource 与另一个 rails 应用程序(都是 2.3.5 版)对话。我想链接到特定资源对象的页面,但似乎没有任何好的方法可以做到这一点。我想出的丑陋方法是在我的 routes.rb 文件中添加一行来模仿我的资源,如下所示:
# environment.rb, or in the config/environments/*.rb files
PERSON_URL = "people.example.com"
# person.rb
class Person < ActiveResource::Base
self.site = "http://#{PERSON_URL}"
end
# routes.rb
map.resources :people # or persons, or whatever
# my_view.html.erb
<%= link_to person.name, person_url(person, :host => PERSON_URL) %>
但这很丑陋。现在,我的应用程序中出现了一条实际上并不存在的额外路线。一定有更好的方法。模型本身是否有任何线索可以为自己获取 url?有人有任何提示吗?谢谢。