有没有办法让路由使用不在预期文件夹中的 View ?
这是我的问题...我正在为我的 Reports
创建一个新 View , 但是我得到 Missing template v1/reports/index
错误。
routes.rb
api_version(module: "V1", path: { value: "v1" }) do
resources :reports, only: [:create, :index]
end
views/reports/index.html.erb
<h3>Just some test content</h3>
controllers/v1/reports_controller.rb
class V1::ReportsController < ApplicationController
def index
@company = current_user.company
reports = if @company
@company.reports.order("created_at DESC")
else
[]
end
end
end
_navigation.html.erb
<%= link_to "Reports", v1_reports_path, role: "menuItem" %>
我用了apipie
为我的应用程序创建一些 API,该应用程序在我的应用程序中的“v1”目录下有许多 Controller ,现在在我扩展我的应用程序时会引起一些问题(我认为)。我不想重做结构,所以希望有一种方法可以引导路由使用 views/reports/
下的 View 而不是 v1/reports/
这是它正在寻找的不存在的东西。
...或者我还必须创建一个“views/v1/reports/index.html.erb”文件?
最佳答案
你应该能够告诉你的 Controller 渲染什么路径
class V1::ReportsController < ApplicationController
def index
@company = current_user.company
reports = if @company
@company.reports.order("created_at DESC")
else
[]
end
end
render '/path/to/views/reports/index.html.erb'
end
有关 render method 的文档,请参阅第 2.2.3 和 2.2.5 节
尽管如此,您应该使用 rails 要求的目录。主要是因为如果 future 有一个 v2 Controller ,你会把它的 View 放在哪里?
关于ruby-on-rails - Rails 路由到另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002811/