是否可以将 Ruby on Rails 应用程序的部分内容渲染到另一个应用程序?

发布于 2024-10-11 03:27:52 字数 584 浏览 5 评论 0原文

我有两个 RoR3 应用程序(APP1APP2

  • www.subdomain1.example.com
  • www.subdomain2.example.com

,我想在 APP1 上显示em>来自APP2的一些观点。

我尝试使用“Net::HTTP”请求(APP1 中的代码)来执行此操作

Net::HTTP.get( URI.parse("http://www.subdomain2.example.com/users/new") )

,但响应未评估为 HTTP 代码。除此之外,我不知道是否还有其他技术可以更简单地完成我想要的事情。

那么,是否可以使用在同一 RoR 应用程序中渲染局部的常见且简单的方法来渲染从 APP1APP2 的局部?

示例:

render :partial => "/users/new"

如果可以,我该怎么做?

I have two RoR3 applications (APP1 and APP2)

  • www.subdomain1.example.com
  • www.subdomain2.example.com

and I want to show on APP1 some views from APP2.

I tried to do that using a 'Net::HTTP' request (code in APP1)

Net::HTTP.get( URI.parse("http://www.subdomain2.example.com/users/new") )

but the response is not evaluated as HTTP code. Among other things I do not know if there are other techniques to do what I want in more easy way.

So, is it possible to render partials from APP1 to APP2 using the common and easy approach of rendering partials in the same RoR application?

Example:

render :partial => "/users/new"

If so, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本宫微胖 2024-10-18 03:27:52

在这里,试试这个:

module ApplicationHelper
  require 'open-uri'

  def render_url(url)
    open url do |f|
      f.read.html_safe   # remove the 'html_safe' if you're on Rails 2.x
    end
  end
end

在你看来:

<%= render_url 'http://ilikestuffblog.com/' %>

它会起作用。但只有一个问题:如果网站包含图像、其他页面或其他任何内容的相对链接,这些链接将无法正确显示。尝试此操作以查看一堆空白图像:

<%= render_url 'http://www.ducklet.com/' %>

另外,请注意,如果您不拥有所包含的 URL,您将受到跨站点脚本编写怪异的影响。

Here, try this:

module ApplicationHelper
  require 'open-uri'

  def render_url(url)
    open url do |f|
      f.read.html_safe   # remove the 'html_safe' if you're on Rails 2.x
    end
  end
end

In your view:

<%= render_url 'http://ilikestuffblog.com/' %>

It will work. Just one problem, though: if the site contains relative links to images, other pages, or anything else, those links will not be shown correctly. Try this to see a bunch of blank images:

<%= render_url 'http://www.ducklet.com/' %>

Also, BE WARNED that if you don't own the URL you're including, you will be subject to cross-site scripting weirdness.

听风吹 2024-10-18 03:27:52

如果两个应用程序共享一个文件系统或可以访问共享文件系统,那么您可以直接通过文件路径引用部分文件系统。来自 Rails 渲染指南:

2.2.4 渲染任意文件

渲染方法也可以使用视图
这完全不属于你的范围
应用程序(也许您正在共享
两个 Rails 应用程序之间的视图):

渲染
“/u/apps/warehouse_app/current/app/views/products/show”

Rails 确定这是一个文件
由于前导斜杠而渲染
特点。为了明确起见,您可以使用
:file 选项(这是必需的
在 Rails 2.2 及更早版本上):

渲染:文件=>
“/u/apps/warehouse_app/current/app/views/products/show”

:file 选项采用绝对值
文件系统路径。当然,你需要
有权认为您是
用于呈现内容。

If the two applications share a filesystem or have access to a shared filesystem, then you can reference a partial directly by file path. From the Rails guide on rendering:

2.2.4 Rendering an Arbitrary File

The render method can also use a view
that’s entirely outside of your
application (perhaps you’re sharing
views between two Rails applications):

render
"/u/apps/warehouse_app/current/app/views/products/show"

Rails determines that this is a file
render because of the leading slash
character. To be explicit, you can use
the :file option (which was required
on Rails 2.2 and earlier):

render :file =>
"/u/apps/warehouse_app/current/app/views/products/show"

The :file option takes an absolute
file-system path. Of course, you need
to have rights to the view that you’re
using to render the content.

乱了心跳 2024-10-18 03:27:52

创建一个包含任何共享代码(即部分代码)的 gem 可能会更谨慎,这样两个应用程序都可以使用它。

It might be more prudent to create a gem that has any shared code (ie. partials) in it so both apps can use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文