2010-04-12 78 views
0

我想在rails中创建一个简单的辅助模块,而且我很难从我的新人形式(app/views/people/new.html)中获取以下错误消息。 erb):Rails帮助模块undefined方法`排序'

undefined method `sort' for 97:Fixnum 

Extracted source (around line #17): 

14: <p> 
15:   <% nations = { 'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico' => 'Mexico', 'United Kingdom' => 'UK' } %> 
16:  <%= f.label :country %><br /> 
17:   <%= radio_buttons(:person, :country, nations) %> 
18:   
19: </p> 
20: <p> 

radio_buttons是我为我的视图创建的帮助器模块。这是(应用程序/佣工/ people_helper.rb):

module PeopleHelper 

def radio_buttons(model_name, target_property, button_source) 
    html='' 
    list = button_source.sort 
    list.each do |x| 
    html << radio_buttons(model_name, target_property, x[1]) 
    html << h(x[0]) 
    html << '<br />' 
    end 
    return html 
end 

end 

的问题似乎是在“名单= button_source.sort”,但我不知道为什么它说的方法是不确定的。我已经能够直接在我的视图代码中使用它。我不能在辅助模块中使用这样的方法吗?我需要包括什么吗?

感谢您的帮助!

回答

1

在行html << radio_buttons(model_name, target_property, x[1])该方法递归调用自身。这一次第三个参数不再是散列,但是这是一个Fixnum,它是x[1]。由于Finxums不响应sort,您会收到错误消息。

0

undefined method 'sort' for 97:Fixnum意味着97,一个Fixnum的实例,没有sort方法。出于某种原因,方法中的参数button_source以整数形式出现。

速战速决是写list = Array(button_source).sort这将迫使button_sourceArray,因为Array s为具有sort方法最常见的类。如果button_source已经是Array,则不会有任何更改。

+0

这让我过去了排序错误,所以谢谢!但是,现在我得到了“堆栈太深”的错误。我的递归似乎是有问题的。 – Magicked 2010-04-12 16:26:34