2014-12-25 111 views
0

我有一个类my_class.rb其具有一个公共方法get_names逃生的方括号和引号或没有引号和括号

class MyClass < ActiveRecord::Base 

    ##.....## 

    def get_names 
    self.students.collect {|x| x.name} 
    end 
end 

在视图上我打电话的方法来填充表单输入取此数组现场是这样的:

<%= simple_form_for @movie do |f| %> 
    ... 

    <%= f.input :get_names %> 
<% end %> 

,填补输入字段看起来像这样的价值观:

["William", "Alexis", "John", "Richard"] 

,但我想他们是这样的:

William, Alexis, John, Richard 

我可以做出哪些改变来get_names方法或我怎么能逃脱括号和引号?

回答

1

您可以在阵列上使用join方法:

["William", "Alexis", "John", "Richard"].join(", ")

所以在这里,你可以这样做:

self.students.collect {|x| x.name}.join(", ")

但随后你的方法将返回一个字符串,而不是一个阵列。

+0

这就是我一直在寻找的感谢。 –