在 rails 应用程序中安装 Sinatra 应用程序并共享布局 [英] Mount Sinatra app inside a rails app and sharing layout

查看:47
本文介绍了在 rails 应用程序中安装 Sinatra 应用程序并共享布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 rails 应用程序中安装一个 sinatra 应用程序.但我希望这个共享相同的布局.

I would like to mount a sinatra application in my rails app. But I would like this one to share the same layout.

iframe 可以工作,但您还有其他想法吗?

The iframe could work but do you have any other idea ?

谢谢

推荐答案

你基本上需要做两件事:

You basically need to do two things:

您需要告诉 Rails 路由器某个 URL 路径将由另一个 Rack 应用程序(在您的情况下是 Sinata 应用程序)处理.这可以通过将其添加到您的 routes.rb 中来完成:

You need to tell the Rails router that a certain URL path is to be handled by another Rack app (in your case a Sinata app). This can be done by adding this to your routes.rb:

match "/sinatra" => MySinatraApp, :anchor => false

完成后,您可以像这样创建您的应用:

Having done that, you can create your app like so:

class MySinatraApp < Sinatra::Base
  get "/" do
    "Hello Sinatra World"
  end
end

现在的第二步是告诉您的 Sinatra 应用程序使用 Rails 布局,该布局默认位于 Rails 3.1 的 app/views/layouts/application.html.erb 中.默认情况下,Sinatra 使用 ./views/layout.ext(ext 是您选择的模板系统的扩展).所以你基本上必须告诉 Sinatra

The second step now is to tell your Sinatra app to use the rails layout which by default lives in app/views/layouts/application.html.erb for Rails 3.1. by default, Sinatra uses ./views/layout.ext (with ext being the extension of your chosen template system). So you basically, have to tell Sinatra to

  1. 使用另一个目录来查找视图和布局,而不是默认的 ./views
  2. 使用另一个模板文件作为默认布局.

都可以通过在您的 sinatra 应用中设置以下内容来实现:

Both can be achieved by setting the following in your sinatra app:

set :views, "/path/to/your/railsapp/views"
set :erb, layout => :"layout/application" # or whatever rendering engine you chose

这篇关于在 rails 应用程序中安装 Sinatra 应用程序并共享布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆