【问题标题】:no implicit conversion of Symbol into Integer, rails 4没有将符号隐式转换为整数,rails 4
【发布时间】:2016-08-03 15:12:24
【问题描述】:

我收到了这个错误,没有将 Symbol 隐式转换为 Integer。我搜索了这个错误,但并不真正理解它。代码在底部。一旦它到达这个“if q[:text_field].is?Array”,那就是它给出错误的时候,我确信该代码的其余部分是错误的。但不知道如何解决。

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

有人可以帮忙吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    这个错误意味着你在一个数组上使用了[],但是你传递了一些对数组没有意义的东西。在这种特殊情况下,它告诉您您尝试用作哈希的 q 实际上是一个数组。

    发生这种情况是因为process_fill_in(*page[:q_fill_in]) 正在将散列转换为键值对数组(因为 *)。我完全不知道你为什么会在那里吐口水。

    【讨论】:

    • 我这样做是因为从 api 数据中您可以看到一个哈希数组,但显然这是错误的。你对我能做些什么来解决它有什么建议吗?
    • 据我所知,你根本不需要那个 splat(也可能不在 process_fill_in 中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2023-03-20
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多