如何使用Ruby on Rails将字符串转换为数组 [英] How to convert a string to an array using Ruby on Rails

查看:131
本文介绍了如何使用Ruby on Rails将字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字符串值的文本字段,例如

I have a text field that takes in a string value, like

"games,fun,sports"

我的主要目的是将字符串转换为如下所示的数组:

My main goal here is to take the string and turn it into a Array like this:

[games, fun, sports]



我拥有的集成对象的过滤器属性中的

。现在,我开始使用的方法似乎无效。

in the filters attribute for the integrations object I have. Right now I have the beginning of a method that doesn't seem to work.

这是我的代码:

查看:

  <%= form_for @integrations, url: url_for(:controller => :integrations, :action => :update, :id => @integrations.id) do |f| %>
   <%= f.label :filters %>
   <%= f.text_field :filters, class: "filter-autocomplete" %>
   <%= f.submit "Save" %>
  <% end %> 

这是接受字符串的文本字段。

That is the text field that takes in the string.

型号:

def filters=(filters)

end

这是我想从字符串切换到数组的地方。

This is the place that I'd like to make the switch from string to array.

控制器:

 def update
    @integrations = current_account.integrations.find(params[:id])

    if @integrations.update_attributes(update_params)
      flash[:success] = "Filters added"
      redirect_to account_integrations_path
    else
      render :filters
    end
  end

  def filters
    @integrations = current_account.integrations.find(params[:id])
  end

  private

  def update_params
    [:integration_webhook, :integration_pager_duty, :integration_slack].each do |model|
      return params.require(model).permit(:filters) if params.has_key?(model)
    end
  end

因此,回顾一下:我有一个集成模型,它接受一串过滤器。我想要一种将字符串分解为过滤器属性元素的方法。

So, recap: I have a integrations model that takes in a string of filters. I want a method that will break up the string into an element of filter attributes.

这是我要添加过滤器的对象:

Here is the object that I'm trying to add the filters to:

对象:

 id: "5729de33-befa-4f05-8033-b0acd5c4ee4b",
 user_id: nil,
 type: "Integration::Webhook",
 settings: {"hook_url"=>"https://hooks.zapier.com/hooks/catch/1062282/4b0h0daa/"},
 created_at: Mon, 29 Aug 2016 03:30:29 UTC +00:00,
 owner_id: "59d4357f-3210-4ddc-9cb9-3c758fc1ef3a",
 filters: "[\"Hey\", \"ohh\"]">

您可以看到过滤器是什么我正在尝试修改。

As you can see the filters is what I'm trying to modify. Instead of this in the object:

"[\"Hey\", \"ohh\"]"

我要这样:

[Hey, ohh]


推荐答案

目前尚不清楚您要做什么,但通常情况下,当您输入以下字符串时:

It's not clear what you're after, but generally, when you have a string like:

"games,fun,sports"

您可以使用 split(',')将其用逗号隔开,并转换为字符串数组:

you can use split(',') to break it on the commas and turn it into an array of strings:

"games,fun,sports".split(',') # => ["games", "fun", "sports"]

如果您接收的是JSON编码字符串数组,看起来像这样:

If you're receiving a JSON encoded array of strings, it'll look like:

'["games", "fun", "sports"]'

又名:

'["games", "fun", "sports"]' # => "[\"games\", \"fun\", \"sports\"]"

可以很容易地返回到Ruby字符串数组:

which can be returned to a Ruby array of strings easily enough:

require 'json'

JSON['["games", "fun", "sports"]'] # => ["games", "fun", "sports"]

这篇关于如何使用Ruby on Rails将字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆