2017-01-22 38 views
0

我有它在我的ApplicationControllerRails的AplicationController麻煩

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    $banner = Banner.first 
    $template = Template.first 
end 

,並在我的_myfile.html.erb

<%= $template.mainpage_benefits.html_safe %> 

我的終端有一個錯誤:

ActionView::Template::Error (undefined method `html_safe' for nil:NilClass 

我不t之前($ template = Template.first)遇到了這種做法。請給我解釋一下,我怎麼能解決這個問題

+0

嘗試使用'raw'方法 – uzaif

+0

檢查爲什麼不使用'@ template'而不是'$ template' – uzaif

回答

0

糾正你的觀點有:

<%= $template.mainpage_benefits ? $template.mainpage_benefits.html_safe : '' %> 

成爲別人的評論中指出,通常,最好在這些情況下使用實例瓦爾(@template)。

+0

它是一個版本代碼,在生產環境中工作,我無法刪除或升級此代碼。爲什麼在生產這個代碼是工作,但在我的本地機器不是。我想在本地啓動這個項目沒有任何改變))) –

+0

$模板 - 對我來說是缺乏類。這裏的問題)) –

+0

嘗試執行'rails console' - 然後:'Template.first' - 從錯誤它看起來好像'templates'表爲空 – Mat