Ruby on Rails 视图不呈现(加载空白)

Ruby on Rails View Does Not Render (Loads Blank)

提问人:Corey Cramer 提问时间:10/10/2023 最后编辑:Corey Cramer 更新时间:10/10/2023 访问量:60

问:

我正在努力在 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.erbapp/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.builderapp/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


我已确认有正在返回的本地种子数据。我显然错过了一些东西,但我不知道是什么。

提前感谢您提供的任何帮助!

Ruby-on-Rails 模型-视图-控制器 RSS

评论

0赞 max 10/10/2023
能解决问题吗?我不是 100% 确定,但我怀疑当您在没有任何选项的情况下显式调用时,它的行为不像隐式渲染。format.rss { render :zillow, layout: false }render
0赞 Corey Cramer 10/10/2023
嘿@max,感谢您的回复!进行此重构不会对响应产生明显影响

答:

1赞 max 10/10/2023 #1

只需在控制器级别设置布局选项并让隐式渲染完成繁重的工作,就可以实现相同的目的:

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

评论

0赞 Corey Cramer 10/10/2023
嘿@max,再次感谢您的反馈!进行此重构会在第 2 行引发以下异常:#<NoMethodError: undefined method 'layout' for FeedsController:Class>
0赞 max 10/10/2023
@CoreyCramer啊,该方法必须由 ActionController::API 中未包含的模块之一提供。让你的控制器继承自 可能更容易。ActionController::Base
0赞 max 10/10/2023
缺少的模块是 ActionView::Layouts
0赞 Corey Cramer 10/10/2023
对控制器继承的位置进行更改。再次感谢你,马克斯!
0赞 Corey Cramer 10/10/2023
此外,应用编辑后的解决方案在仍继承自 API 的同时有效。将此标记为下一个人的正确答案!