ruby-on-rails Ruby on Rails视图不渲染(加载空白)

vd8tlhqk  于 2023-10-21  发布在  Ruby
关注(0)|答案(1)|浏览(139)

我正致力于在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

我已确认有正在返回的本地种子数据。很明显我漏掉了什么但我不知道是什么。
提前感谢提供的任何帮助!

aurhwmvo

aurhwmvo1#

你可以通过在控制器级别设置布局选项并让隐式渲染完成繁重的工作来实现同样的事情:

class FeedsController < ApplicationController
  include ActionView::Layouts
  layout false, only: [:rss]
  
  def zillow
    @listings = Listing.where(syndication_type: 'SYNDICATION_ON')
    respond_to :html, :rss
  end
end

相关问题