在Rails中将对象的属性转换为数组

时间:2021-02-28 22:30:45

I have a form in my Rails application, and in the form, there is a textbox that corresponds to :mylist, which is an attribute of my class Catcher's object @mycatcherobject In the form, people will paste something that looks like this:

我在我的Rails应用程序中有一个表单,在表单中,有一个对应于:mylist的文本框,它是我的类的属性Catcher的对象@mycatcherobject在表单中,人们将粘贴如下所示的内容:

FRI.9234.43.32340.pdf

FRI.9234.43.32340.pdf

NNY.6042.43.5523.pdf

NNY.6042.43.5523.pdf

LJN.9867.56.1659.pdf

LJN.9867.56.1659.pdf

After the form is submitted, I need to turn what was pasted into that text box (that is, what is now the value of :mylist) into an array. For the above example, there would be three elements in the array (one for each of the items above).

提交表单后,我需要将粘贴到该文本框中的内容(即,现在的值:mylist)转换为数组。对于上面的示例,数组中将有三个元素(上面的每个项目都有一个元素)。

Do you have any idea how to do this? I can't figure out how to convert an object's attribute's value into an array anywhere.

你知道怎么做吗?我无法弄清楚如何将对象的属性值转换为数组。

I've tried things like %w(#{@mycatcherobject.mylist}), but that doesn't work.

我尝试了%w(#{@ mycatcherobject.mylist})之类的东西,但这不起作用。

I'm fairly new at Rails, so my apologies if this seems obvious. Your help would be greatly appreciated.

我在Rails上相当新,所以如果这看起来很明显,我很抱歉。非常感谢您的帮助。

1 个解决方案

#1


0  

That comes in the controller as a param[:name of the field] and you can write a regular expression to split the string. In case the elements are separated by a comma or a special character you can do something like

它在控制器中作为参数[:字段的名称],您可以编写正则表达式来拆分字符串。如果元素由逗号或特殊字符分隔,您可以执行类似的操作

result = params[:list].split(",")

#1


0  

That comes in the controller as a param[:name of the field] and you can write a regular expression to split the string. In case the elements are separated by a comma or a special character you can do something like

它在控制器中作为参数[:字段的名称],您可以编写正则表达式来拆分字符串。如果元素由逗号或特殊字符分隔,您可以执行类似的操作

result = params[:list].split(",")