Rails 5 api仅适用于主页

kot*_*u b 9 api ruby-on-rails-5

我已生成rails 5 api应用程序.但我希望我的应用程序与主页.为此,我生成了一个家庭控制器并添加了视图文件i各自的views/home/index.html.erb

但当我尝试访问它时,我得到的反应低于预期

在2016-07-14 11:14:03开始GET"/ home/index"为127.0.0.1 +0530由HomeController处理#index为HTML已完成204无内容在0ms

在2016-07-14 11:14:20开始获取"/home/index.js"for 127.0.0.1 +0530由HomeController处理#index为JS已完成204无内容在0ms

但我看不到网页上显示的索引页面内容.

请分享您的想法.

Ris*_*ori 17

我在同一条船上,试图做一个Rails 5 API应用程序仍然可以从单个html页面引导(由JS在加载时接管).从rails源中窃取提示,我创建了以下控制器(请注意,ApplicationController对于这个单独的非api控制器,它使用Rails而不是my )

require 'rails/application_controller'

class StaticController < Rails::ApplicationController
  def index
    render file: Rails.root.join('public', 'index.html')
  end
end
Run Code Online (Sandbox Code Playgroud)

并将相应的静态文件(plain .html,not .html.erb)放在该public文件夹中.我还补充道

get '*other', to: 'static#index'
Run Code Online (Sandbox Code Playgroud)

routes.rb(在我的所有api路由之后)结束时,为重新加载,深层链接等保留客户端路由.

如果没有设置root权限routes.rb,Rails将在调用时直接从公共服务/,否则将在非api路由上命中静态控制器.根据您的用例,添加public/index.html(没有root routes.rb)可能就足够了,或者您可以StaticController通过使用来实现类似的东西而不会出现奇怪的情况

get '*other', to: redirect('/')
Run Code Online (Sandbox Code Playgroud)

相反,如果你不关心路径保存.

我很想知道是否有其他人有更好的建议.