我正在尝试为Ruby on Rails开发一个插件,并遇到了渲染我的html视图的问题。我的目录结构如下:
---/vendor
|---/plugins
|---/todo
|---/lib
|---/app
|---/controllers
|---todos_controller.rb
|---/models
|---todos.rb
|---/views
|---index.html.erb
|---todo_lib.rb
|---/rails
|---init.rb
require 'todo_lib'
%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
class TodosController < ActionController::Base
def index
end
end
<!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>
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.
map.resources :todos
...
我得到的错误如下:
在视图路径app / views中缺少模板todos / index.erb
任何人都可以举手告诉我这里我做错了什么导致我的index.html.erb文件无法呈现?非常感谢!
我已经尝试了以下但没有成功:
def index
respond_to do |format|
format.html # index.html.erb
end
end
hakunin 解决了这个问题。这是解决方案。
他说我正在构建一个Rails引擎插件(我不知道我是这样做的),它需要一个不同的目录结构,如下所示:
---/vendor
|---/plugins
|---/todo
|---/lib
|---/app
|---/controllers
|---todos_controller.rb
|---/models
|---todos.rb
|---/views
|---/todos
|---index.html.erb
|---todo_lib.rb
|---/rails
|---init.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' ,dir))。 [忽略加粗的“文件”,这是网站的问题]。
运行脚本/服务器将在todo / app / views / todos下呈现index.html.erb页面。
答案 0 :(得分:1)
看起来你想构建一个“引擎”插件。在插件目录的根目录中创建“app”和“config”目录(不在/ lib下)。您可以在插件中使用app / views /和app / controllers,就像它是一个功能齐全的Rails应用程序一样。在config / routes.rb中,您应该声明引擎引入的路由。
请参阅http://github.com/neerajdotname/admin_data了解发动机的样子。