0

我有一个在两个域上运行的 Rails 应用程序,domain1并且domain2. 每个域都有自己的关联博客,domain1blogdomain2blog,分别。目前,我有一个模型可以获取其中一个博客:

require 'rss'
require 'open-uri'

class Blog

BLOG_URL = 'http://feeds.feedburner.com/domain1blog?format=xml'
POST_LIMIT = 2

def self.fetch_entries
  Rails.cache.fetch('blog', expires_in: 10.minutes) do
    posts = []

    begin
      open BLOG_URL do |rss|
        feed = RSS::Parser.parse(rss)
        posts = feed.items.slice 0...POST_LIMIT
      end
    rescue OpenURI::HTTPError
    # Ignore silently.
    end

    posts
  end
end

end

现在,domain1blog无论从哪个域进行调用,此模型都会从其中获取内容。

如何设置BLOG_URL它以使其指向domain1blogfromdomain1domain2blogfrom domain2

4

1 回答 1

0

你可以

  • 在数据库中创建Blog具有(预定义)url字段的实体,这意味着您的fetch_entries方法将开箱即用(尽管作为实例方法)
  • 或提取控制器中的域,使用ActionDispatch::Http::URL.domain
于 2015-03-26T05:36:18.513 回答