3

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

/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
  ...

私が得るエラーは次のとおりです。

テンプレートがありません

ビューパスアプリ/ビューにテンプレート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'、dir))。[太字の「ファイル」は無視してください。これはWebサイトの問題です]。

script / serverを実行すると、todo / app / views/todosの下にindex.html.erbページが表示されます。

4

1 に答える 1

1

「エンジン」プラグインを作成したいようです。プラグインディレクトリのルート(/ libの下ではない)に「app」および「config」dirを作成します。プラグインでは、フル機能のRailsアプリであるかのようにapp /views/およびapp/controllersを使用できます。config / routers.rbで、エンジンによって導入されたルートを宣言する必要があります。

エンジンがどのように見えるかの適切な例については、 http: //github.com/neerajdotname/admin_dataを参照してください。

于 2009-12-06T22:21:53.203 に答える