这是我仅在 Chrome 中遇到的错误,并且仅发生在特定路线 -/dashboard/friends 上。 html 显示为纯文本,包含在
内<pre style="word-wrap: break-word; white-space: pre-wrap;">
如果我对与 View 关联的文件的内容进行任何更改,例如简单地将“blah”放入index.haml 文件中,它将正确呈现。有人知道可能是什么原因造成的吗?
编辑:这是在浏览器中呈现的内容 -
<html>
<head>
</head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">
"my apps view renders in here including the head and html,
but since it is in quotes, it just appears as plain text
<!DOCTYPE>
<html>
entire rails app html here...
</html>"
</pre></body></html>
该错误似乎与我在 Controller 中的 @invites 变量有关,删除它可以解决问题 -
class Dashboard::FriendsController < Dashboard::BaseController
def index
@friends = current_user.friends
#removing the @invites var fixes it, but I need those invitations...
@invites = current_user.invitations.where(type: "FriendInvite").includes(:recipient)
end
end
在index.haml中
%header.page-hdr
%h1
Friends
%ul
%li
= link_to dashboard_find_friend_path, class: "btn" do
.fa.fa-plus
Add a Friend
.row
- if @friends.empty?
You have no friends
- else
- @friends.each do |friend|
= render 'friend', friend: friend
- if @invites.present?
%h2
Invited
- @invites.each do |inv|
= render partial: 'invite', locals: {inv: inv}
这是layout.haml -
!!! 5
%html
%head
%title= full_title(yield(:title))
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "application", "data-turbolinks-track" => true
= csrf_meta_tags
= favicon_link_tag '/favicon.ico'
= favicon_link_tag '/favicon.png', rel: 'apple-touch-icon', type: 'image/png'
%script{ type: "text/javascript", src: "//use.typekit.net/gey0efl.js" }
%script{ type: "text/javascript"}
try{Typekit.load();}catch(e){}
%body#dashboard-body
= render current_user.navigation
#dashboard-wrapper
= render "shared/dashboard/header"
= yield
更新:升级到 Rails 4.1,没有运气。再次仅在 Chrome 中进行中断,并且仅当我包含第二个实例变量并且该变量已以某种方式修改(添加了新记录)
最佳答案
事实证明,问题是正在渲染的文件缺少扩展名。我所有的观点都是 .haml 文件,将它们更改为 .html.haml 解决了这个问题。
关于ruby-on-rails - Rails 将 html 渲染为 pre 元素内的纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24066088/