在控制器操作中从公共服务文件

问题描述 投票:1回答:3

在我的项目中,我们有一个单页面应用程序,我们的构建过程将应用程序编译为public/index.html中的单个静态文件。

单页应用程序负责处理应用程序中的所有路由,因此无论您是访问site.com还是site.com/foo/bar/actionsite.com/☃,我都希望Rails应用程序能够为public/index.html提供服务

我有这条路线:

match '*path', to: 'foo#index', via: :all

在FooController中:

class FooController < ApplicationController
  def index
    render 'index.html', layout: false
  end
end

这不像我希望的那样有效;我得到错误Missing template foo/index.html, application/index.html

作为Rails控制器操作的一部分,是否可以从public提供资产?

ruby-on-rails-4 asset-pipeline
3个回答
4
投票

在Rails 5中,我通过这样做得到了这个

render file: "public/examplename.html", layout: false

0
投票

这比我想象的要容易得多。

我变了:

render 'index.html', layout: false

render 'public/index.html', layout: false

一切都按预期工作。


0
投票

这似乎已经改变了。我正在尝试暂时为我的应用程序的管理部分呈现静态文件,这将是一个通过API与rails通信的VueJS SPA。我必须做以下事情:

的routes.rb

  match '*path', :to => 'console#index', :constraints => { :subdomain=>'console'}, :via=>:all

console_controller.rb

class ConsoleController < ApplicationController
  def index
    render :file=>'/public/console/index.html', :layout=>false
  end
end

所以在路径上使用:file而不是没有参数 - 因为否则Rails 5试图将其视为普通视图模板。

通过该路径匹配和子域,这看起来效果很好,因为所有路由都将传递到VueJS路由器。

© www.soinside.com 2019 - 2024. All rights reserved.