簡體   English   中英

在構建 Rails Gem 時,如何讓 Rails 在查找視圖部分時加載的 ActionView 路徑中包含我的 gem 的 app/views/?

[英]When building a Rails Gem, how do I get Rails to include my gem's app/views/ in the ActionView paths that it loads when looking for view partials?

我正在建造一顆寶石。

你可以在這里看到我的超級簡單 gem: https://github.com/jasonfb/xyz_gem

它只是一個 shell。

今天我想知道的是什么告訴Rails它應該加載我的Gem的視圖,即加載partials時app/views/中的視圖?

我有一個名為 XyzGemTest (不是 gem)的 rails 應用程序,它通過本地路徑使用 gem 包括

我認為我只是在 HellosController 上進行渲染以進行演示。

該視圖呈現:

this is in the app code
<%= render partial: 'hello_world' %>

請注意,我的部分hello_world在我的 Gem 中——在 app/views/_hello_world.erb 中(盡管我也嘗試將它移動到 app/views/xyz_gem/_hello_world.erb——見下文)

當我加載我的應用程序時,我收到了這個常見錯誤:

在此處輸入圖像描述

如您所見,這個空白的 Rails 6.0 應用程序正試圖從 3 個地方加載視圖文件:(1)我自己的應用程序的 app/views 文件夾(不是 gem)(2)actiontext(3)actionmailbox

我在這里要問的是我需要對我的 Gem 做些什么來制作它,以便 ActionView 將我的 gem 的 app/views/ 文件夾包含在它查找的路徑“鏈”中?

我嘗試了 app/views/_hello_world.erb 並將 _hello_world.erb 嵌套到一個名為 xyz_gem 的文件夾中,就像這樣

在此處輸入圖像描述

歡迎您分叉和重構我上面的 Github 存儲庫。

加載路徑?

application.rb 有一行config.autoload_paths

我認為您需要查看那里才能找到答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM