Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RoR-基于请求域获取博客提要_Ruby On Rails_Ruby On Rails 3_Multiple Domains_Dynamic Url - Fatal编程技术网

Ruby on rails RoR-基于请求域获取博客提要

Ruby on rails RoR-基于请求域获取博客提要,ruby-on-rails,ruby-on-rails-3,multiple-domains,dynamic-url,Ruby On Rails,Ruby On Rails 3,Multiple Domains,Dynamic Url,我有一个运行在两个域上的Rails应用程序,domain1和domain2。每个域都有自己的关联博客,分别是domain1blog和domain2blog。目前,我有一个模型可以获取其中一个博客: require 'rss' require 'open-uri' class Blog BLOG_URL = 'http://feeds.feedburner.com/domain1blog?format=xml' POST_LIMIT = 2 def self.fetch_entries

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

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获取内容,而不管调用来自哪个域

如何设置博客URL,使其指向来自domain1的domain1blog和来自domain2的domain2blog?

您可以

或者在数据库中创建带有预定义url字段的博客实体,这意味着您的fetch_entries方法将作为一个实例方法即时可用 或在控制器中提取域,使用