[英]Getting error ActionView::Template::Error (undefined method `each' for nil:NilClass)
[英]ActionView::Template::Error (undefined method `each' for nil:NilClass):?
我在 Rails 項目的 ruby 中有一個服務model但我無法在我的項目的 pages#home 視圖中呈現它我的服務控制器-
我的 ServicesController 文件-
class ServicesController < ApplicationController
def show
@services = Service.all
end
end
我的節目文件-
<div class="container">
<h3>Services we offer-</h3>
<% @services.each do |service| %>
<h5><%= service.name %></h5>
<p><%= service.description %></p>
<% end %>
</div>
我的服務視圖顯示文件的路徑是這個- app\views\services\_show.html.erb
我在我的頁面視圖中渲染它app\views\pages\home.html.erb
as <%= render 'services/show' %>
,盡管我可以使用 @services=Service.all 命令在 rails 控制台中查看所有服務,但我仍然收到錯誤消息。
有人可以幫我嗎?
您在錯誤的 controller 和操作中設置了變量。
如果您希望@services
實例變量在home.html.erb
視圖中可用,則需要在PagesController#home
中設置該數據。
如果您在正確的操作中設置變量,然后在home
視圖中呈現部分內容,無論是_show
還是另一個,您將能夠訪問該變量。
嘗試類似:
class PagesController < ApplicationController
...
def home
@services = Service.all
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.