2016-08-02 27 views
0

我一直在收到這個錯誤,沒有將Symbol隱式轉換爲Integer。我搜索了這個錯誤,但並沒有真正理解它。代碼在底部。一旦它到達這個「if q [:text_field] .is?Array」,那就是當它給出錯誤時,我確信該代碼的其餘部分是錯誤的。但不知道如何解決它。沒有將Symbol隱式轉換爲Integer,rails 4

pages = Vovici::API.new(@pid).survey_structure 

這是我用上面的代碼調用的api數據的示例。

[{:q_fill_in=> 
{:heading=>{:text=>"1"}, 
    :instructions=>{:text=>nil}, 
    :body=>{:text=>"Pac"}, 
    :list_caption=>{:text=>{:@type=>"plain"}}, 
    :total_label=>{:text=>"Total"}, 
    :text_field=> 
    [{:label=>{:text=>"first"}, 
    :preselection=>{:text=>{:@type=>"plain"}}, 
    :symbol=>{:text=>{:@type=>"plain"}}, 
    :@id=>"1", 
    :@dbheading=>"Q1_1", 
    :@row=>"0", 
    :@size=>"20", 
    :@xmltype=>"text", 
    :@required=>"false", 
    :@compare_expression=>"-1", 
    :@topic_first=>"true", 
    :@slider=>"false", 
    :@sliderstep=>"1", 
    :@published=>"true", 
    :@usecalendarpopup=>"true", 
    :@insert_symbol_left=>"false", 
    :@label_width=>"3", 
    :@text_area_width=>"9"}, 
    {:label=>{:text=>"id"}, 
    :preselection=>{:text=>{:@type=>"plain"}}, 
    :symbol=>{:text=>{:@type=>"plain"}}, 
    :@id=>"2", 
    :@dbheading=>"Q1_2", 
    :@row=>"0", 
    :@size=>"20", 
    :@xmltype=>"text", 
    :@required=>"false", 
    :@compare_expression=>"-1", 
    :@topic_first=>"true", 
    :@slider=>"false", 
    :@sliderstep=>"1", 
    :@published=>"true", 
    :@usecalendarpopup=>"true", 
    :@insert_symbol_left=>"false", 
    :@label_width=>"3", 
    :@text_area_width=>"9"}], 
    :@dbheading=>"Q1"} 

這是代碼從我的RB文件

def process 
    pages = Vovici::API.new(@pid).survey_structure 
    pages.each do |page| 
    if page[:q_fill_in] 
    process_fill_in(*page[:q_fill_in]) 
    end 
    end 
end 

def process_fill_in(*questions) 
    questions.each do |q| 
    if q[:text_field].is? Array 
     sub_qs = q[:text_field] 
    else 
     sub_qs = [q[:text_field]] 
    end 
    q_text = clean_question_text(q[:body][:text]) 
    sub_qs.each do |sq| 
     sub_text = clean_question_text(sq[:label][:text]) 
     q_name = [q_text, sub_text.reject { |i| i.nil? || i.empty? }.join("--")] 
     @survey.questions.create!(qheader: sq[:@dbheading], qtext: q_name) 
    end 
    end 
end 

def clean_question_text(text) 
    match = /(&nbsp;)?(<.*?>)?(.+)(&nbsp;)?(<\/.*>)?/.match(text) 
    match[3] 
end 

任何人都可以請幫助?

回答

0

這個錯誤意味着你已經在一個數組上使用了[],但是你傳遞了一些對數組沒有意義的東西。在這個特殊情況下,它告訴你,你試圖用作散列的q實際上是一個數組。

發生這種情況是因爲process_fill_in(*page[:q_fill_in])將散列轉換爲鍵值對(由於*)的數組。我不知道爲什麼你有一個圖標。

+0

我這樣做是因爲從api數據中看到了一組哈希值,但顯然這是錯誤的。你有什麼建議,我可以做些什麼來解決它。 –

+0

據我可以告訴你根本不需要該圖示(也可能不在process_fill_in中)。 –

相關問題