2012-02-14 78 views
2

我在lib /中創建了一個名爲br_states.rb的文件,當我在我的視圖中調用狀態時,我收到以下錯誤消息:Rails 3 - lib未初始化的常量ActionView :: CompiledTemplates :: STATES

uninitialized constant ActionView::CompiledTemplates::STATES 

我已經在我的application.rb中設置了autoload_path來加載lib文件夾,但還沒有工作。 config.autoload_paths << File.join(config.root, "lib")

這是我的代碼:https://gist.github.com/1822459

什麼問題?

回答

2

您可能需要爲該常量提供一個名稱空間。我認爲Rails的的自動裝載程序無法弄清楚,你STATES常數在一個叫br_states.rb文件中定義:

# lib/states.rb 
module States 
    BRAZIL = [ 
    # ... 
    ] 
end 

我也建議以下gg_s的回答是:

# new.html.erb 
<%= adr.select :state, options_for_select(States::BRAZIL), :label => "Estado", :required => true %> 
1

使用options_for_select

<%= adr.select :state, options_for_select(STATES), :label => "Estado", :required => true %> 

請注意,Rails會使用每個兩元素數組是這樣的:[option text, option value],這意味着你的窗體的select將全面「AC」的,「AL」,...並會提交「英畝」,「阿拉戈斯」,...

我想你想讓它顯示全文並返回縮寫。切換您的STATES陣列中的值:

STATES = [[ "Acre",  "AC" ], 
      [ "Alagoas", "AL" ], 
      [ "Amazonas", "AM" ], 
      [ "Amapá", "AP" ], 
      # and so on... 
0

在縮寫文件,行18缺少一組雙引號。這可能不是這個特定問題的根源,但它肯定會造成問題。

相關問題