我正在致力于在 Ruby on Rails 中实现 RSS 提要来联合房地产列表(首先是 Zillow,然后是其他)。目前我正在努力加载视图。
我尝试搜索SO,看看另一个问题是否已关闭或答案是否有效,但结果却是空白。因此,如果这是多余的,但这是无意的,我深表歉意。
Ruby on Rails 版本:7.0.4
路线:
require 'sidekiq/web'
require 'sidekiq/cron/web'
Rails.application.routes.draw do
if ENV['ENABLE_INTERNAL_ROUTES'] || Rails.env.development?
mount Sidekiq::Web => '/sidekiq'
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
end
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql" if ENV['APP_ENV'] != 'production'
post "/graphql", to: "graphql#execute"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
# root "articles#index"
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
get '/feeds/zillow', to: 'feeds#zillow'
end
控制器: 饲料控制器
app/controllers/feeds_controller.rb
class FeedsController < ApplicationController
def zillow
@listings = Listing.where(syndication_type: 'SYNDICATION_ON')
respond_to do |format|
format.html { render "feeds/zillow" }
format.rss { render layout: false }
end
end
end
应用程序控制器
app/controllers/application_controller.rb
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
浏览次数: zillow.html.erb
app/views/feeds/zillow.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Temporary for checking</title>
</head>
<body>
<h1>Do we even exist?</h1>
</body>
</html>
zillow.rss.builder
app/views/feeds/zillow.rss.builder
# app/views/posts/index.xml.builder
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0" do
xml.channel do
xml.title "RSS Rails"
xml.description "Some random description. We will expand later"
end
end
要求:
curl -i http://localhost:3000/feeds/zillow.rss
回应:
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 0
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Content-Type: application/rss+xml; charset=utf-8
Set-Cookie: _session_id=jONgfsG5hiFzPuehEmFthv9vl8l1YATsyC912Ijjr7xZxO8YqQugUfxQt2S%2Bgi1W5s%2BBRxn4YbjxO9OSODjoxrCe7lf9lYUlVK7fnWaPAiBq1Wl49JJtarxeij8S809cUQ%2By4lXDEQKoeAn7s1ru3VK2SF%2FcB5PmgkO%2BLSfLfnfxd3s%3D--uQrBRqJR724ukUkt--xOyV9DgdzMPUplOFYye1FA%3D%3D; path=/; HttpOnly; SameSite=Lax
ETag: W/"36a9e7f1c95b82ffb99743e0c5c4ce95"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: cc95c787-c0fb-42ab-9f80-2ab2fe57182a
X-Runtime: 0.014212
Server-Timing: start_processing.action_controller;dur=0.01, process_action.action_controller;dur=0.56
Vary: Origin
Transfer-Encoding: chunked
%
搜索告诉我返回的单个
%
表示响应结束。值得注意的是,这会下载一个完全空的文件,而不是提供一个空白页面。
如果我从 FeedsController 中删除导致
.rss
请求不渲染布局的指令,我会得到以下信息:
要求:
curl -i http://localhost:3000/feeds/zillow.rss
回应:
HTTP/1.1 204 No Content
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 0
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Set-Cookie: _session_id=MjmWc%2BJ%2FxvWdCymjPQxsJdNgZXkeyGs3ziJTk5lsiLTpqrLNzvYBIKDwYTtjDIBoNbBT%2FN05ukgfsns05AC9rfnafg2A%2BpF9UqFZlR7xUZNtN2xn%2BxdcpXBz7nJMRR2G6S7oH%2FfZcwEnImq5hM50iPOwugvewRjrh78no5V9Z1gsUWg%3D--XH8SskCjhWEW3pRV--bxxYHh8%2FJQeBMCm9VcmrNw%3D%3D; path=/; HttpOnly; SameSite=Lax
Cache-Control: no-cache
X-Request-Id: 84bae277-3507-48b4-b61f-51363058f6e6
X-Runtime: 0.466036
Server-Timing: sql.active_record;dur=8.88, start_processing.action_controller;dur=0.01, process_action.action_controller;dur=18.36
Vary: Origin
要求:
curl -i http://localhost:3000/feeds/zillow
回应:
HTTP/1.1 204 No Content
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 0
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Set-Cookie: _session_id=YDL5FHg4A4jbIXfqTz6kw6FSyVOLfkDd9l7r6kdMMicqtZJRW4ggkULeTu5Nv6RJpNH36TLxIHdTp6%2BxQV6PKGLTR30IyamNzjMUij2IJKhBi%2B8aMsk6fzR4HNpjmQ%2Fh0pIVJvtX7JD25ROWDPbp4Ay6Y22BDSSLpvLApMWvFlTxeOQ%3D--nVZ3vQfPxCDfn%2BO2--yjA0VUMoEvACtvzROGzQ9Q%3D%3D; path=/; HttpOnly; SameSite=Lax
Cache-Control: no-cache
X-Request-Id: 036d1ad5-1b7c-4d45-929b-9d676eb02092
X-Runtime: 0.012600
Server-Timing: start_processing.action_controller;dur=0.01, process_action.action_controller;dur=0.52
Vary: Origin
我已确认有正在返回的本地数据播种。我显然错过了一些东西,但我不知道是什么。
预先感谢您提供的任何帮助!
您只需在控制器级别设置布局选项并让隐式渲染完成繁重的工作即可实现相同的效果:
class FeedsController < ApplicationController
layout false, only: [:rss]
def zillow
@listings = Listing.where(syndication_type: 'SYNDICATION_ON')
respond_to :html, :rss
end
end