简体   繁体   中英

Escape the square brackets and quotation marks or fetch this array without the quotes and brackets

I have a class my_class.rb which has a public method get_names

class MyClass < ActiveRecord::Base

  ##.....##

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

on the view I am calling the method to fill a form input field like this:

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

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

the values that fill the input field looks like this:

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

but i would like them to look like this:

 William, Alexis, John, Richard

What changes can i make to get_names method or how can i escape the brackets and quotation marks?

You can use the join method on your array :

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

So here you can just do :

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

but your method will then return a string and not an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM