我正在尝试为 Ruby on Rails 开发一个插件,但在渲染 html View 时遇到了问题。我的目录结构如下所示:
文件结构
---/vendor
|---/plugins
|---/todo
|---/lib
|---/app
|---/controllers
|---todos_controller.rb
|---/models
|---todos.rb
|---/views
|---index.html.erb
|---todo_lib.rb
|---/rails
|---init.rb
在/rails/init.rb中
require 'todo_lib'
在/lib/app/todo_lib.rb中
%w{ models controllers views }.each do |dir|
# Include the paths:
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/lib/app/models
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/lib/app/controllers
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/lib/app/views
path = File.expand_path(File.join(File.dirname(__FILE__), 'app', dir))
# We add the above path to be included when Rails boots up
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
在 todo/lib/app/controllers/todos_controller.rb
class TodosController < ActionController::Base
def index
end
end
在todo/lib/app/views/index.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Todos:</title>
</head>
<body>
<p style="color: green" id="flash_notice"><%= flash[:notice] %></p>
<h1>Listing Todos</h1>
</body>
</html>
在/myRailsApp/config/routes.rb
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.
map.resources :todos
...
我得到的错误如下:
模板丢失
View 路径 app/views 中缺少模板 todos/index.erb
任何人都可以帮我一下并告诉我我在这里做错了什么导致我的index.html.erb 文件无法渲染吗?非常感谢!
<小时/>编辑:
我已经尝试过以下方法但没有成功:
在/todo/lib/app/controllers/todos_controller.rb
def index
respond_to do |format|
format.html # index.html.erb
end
end
编辑:
白忍解决了这个问题。这是解决方案。
他说我正在构建一个 Rails 引擎插件(我不知道我在这样做),并且它需要不同的目录结构,如下所示:
文件结构
---/vendor
|---/plugins
|---/todo
|---/lib
|---/app
|---/controllers
|---todos_controller.rb
|---/models
|---todos.rb
|---/views
|---/todos
|---index.html.erb
|---todo_lib.rb
|---/rails
|---init.rb
这需要进行以下更改:
在todo/lib/todo_lib.rb
%w{ models controllers views }.each do |dir|
# Include the paths:
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/app/models
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/app/controllers
# /Users/Me/Sites/myRailsApp/vendor/plugins/todo/app/views
path = File.expand_path(File.join(File.dirname(__FILE__), '../app', dir))
# We add the above path to be included when Rails boots up
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
上面所做的更改位于以下行中:path = File.expand_path(File.join(File.dirname(FILE), '../app',目录))。 [忽略粗体的"file",这是网站的问题]。
运行 script/server 将在 todo/app/views/todos 下呈现 index.html.erb 页面。
最佳答案
看起来您想构建一个“引擎”插件。在插件目录的根目录中创建“app”和“config”目录(不在/lib 下)。您可以在插件中使用 app/views/和 app/controllers,就像它是一个功能齐全的 Rails 应用程序一样。在 config/routes.rb 中,您应该声明引擎引入的路由。
参见http://github.com/neerajdotname/admin_data有关引擎外观的一个很好的示例。
关于ruby-on-rails - 自定义插件中的渲染 View 问题(ActionView::MissingTemplate ...错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1856791/