2011-12-05 105 views
0

我目前正在構建一個數據生成器。首先我要實現的是PESEL(根據出生日期波蘭種類的個人ID)生成器 - 我想在表單中輸入一個具有開始和結束出生日期間隔的臨時數據 - 我不想將它存儲在數據庫中(或我我應該這樣做) 這是我PESEL控制器:如何通過數組傳遞變量而不是db記錄?

def new 
    @pesel = Array.new 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @pesel } 
    end 
    end 

,但我有一個「未定義的方法`MODEL_NAME」的NilClass:類」?錯誤 - 它是一個很好的方式反正solvint的這種情況下?我讀過一些使用臨時變量的地方,而不是'The Ruby Way' - 如果我的解決方案不對,請建議正確的。 (???e.g通過這個瓦爾通過cookie散列的helper方法)

這裏是堆棧跟蹤(我認爲):

Started GET "/pesel" for 127.0.0.1 at 2011-12-05 16:18:20 +0100 
    Processing by PeselController#new as HTML 
Rendered pesel/new.html.erb within layouts/application (1513.9ms) 
Completed 500 Internal Server Error in 1793ms 

ActionView::Template::Error (undefined method `model_name' for NilClass:Class): 
    1: <%= simple_form_for @pesel do |f| %> 
    2: <%= f.input :date_of_birth, :as => :date, :start_year => Date.today.year - 90, 
    3:         :end_year => Date.today.year - 12, :discard_day => true, 
    4:         :order => [:month, :year] %> 
    app/views/pesel/new.html.erb:1:in `_app_views_pesel_new_html_erb__708648673_90148530' 
    app/controllers/pesel_controller.rb:7:in `new' 

Rendered /home/ofca/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.6ms) 
Rendered /home/ofca/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (4.0ms) 
Rendered /home/ofca/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (17.6ms) 
+1

你需要給我們更多關於這個錯誤的信息。你能給我們完整的堆棧跟蹤嗎?這通常會告訴我們哪一行代碼實際上正在中斷 - 這對我們有很大的幫助。另外 - 它告訴你,某處很重要。某處不應該有一個零。仔細想想可能是什麼原因造成的。 –

+1

檢查http://ruby-doc.org/core-1.9.2/Struct.html它可以簡化構建模型 – Bohdan

回答

1

form_for假設你通過它的對象存在一定的屬性,如model_name

  1. 而不是使用form_for @pesel,只需用form_tag和相關_tag方法。
  2. 使用Pesel模型。模型是而不是表,並且您的模型不必向數據庫寫入任何內容。只是不要從ActiveRecord繼承,但確實提供了一個model_name以及任何其他字段form_for助手的期望。