将原始文件内容显示为 Github
单击“查看原始”链接后显示文件时,如何像 GitHub 中那样显示原始文件内容?
例如,我想显示 *.html
文件的源代码,但 Rails 在 params[:format]
中采用 html
并以自己的方式呈现。
我怎样才能做到这一点?
How could I display the raw file content as done in GitHub when displaying the file after clicking a "view raw" link?
E.g. I wanted to diplay *.html
file's source but rails takes html
in params[:format]
and renders in its own way.
How could I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我如何让它发挥作用,尽管我不太确定您希望如何在您的应用程序中使用它。将此视为一个概念证明,希望可以帮助您实现目标。
假设如果有人请求“文本”格式,您希望为产品控制器呈现索引操作的原始内容:
这显然不理想,因为您将视图的“原始”版本填充到变量中,即使有人请求正常的 html 格式,但将其放入 format.text 块中会弄乱 index.html.erb 内部分内容的内容类型。再说一遍,这只是我提出的概念证明。
无论如何,现在当您点击:
您将获得页面的原始 HTML。如果你点击:
它将在浏览器中呈现正常的、解释的 HTML。
Here's how I got this to work, though I am not exactly sure how you would want to use this in your application. Consider this a proof of concept that hopefully helps you achieve your goal.
Let's say you want to render the raw contents of the index action for your products controller if someone requests the "text" format:
This obviously isn't ideal since you're stuffing the "raw" version of the view in a variable even if someone requests the normal html format, but putting it in the format.text block screws up the content type for the partials inside index.html.erb. Again, this is just a proof of concept I came up with.
At any rate, now when you hit:
You will get the raw HTML of the page. And if you hit:
It will render the normal, interpreted HTML in the browser.