4

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.

Here is my code:

View:

  <%= 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.

Model:

def filters=(filters)

end

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

Controller:

 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:

Object:

 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\"]"

I would like this:

[Hey, ohh]
1
  • 2
    [games, fun, sports] and [Hey, ohh] would be an illegal array definition unless games, fun, sports, Hey and ohh are variables. Commented Sep 5, 2016 at 21:29

3 Answers 3

14

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

"games,fun,sports"

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"]

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

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

AKA:

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

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

require 'json'

JSON['["games", "fun", "sports"]'] # => ["games", "fun", "sports"]
Sign up to request clarification or add additional context in comments.

2 Comments

The Rails to_json method is pretty handy and always present.
@tadman to_json generates a string from an array, but it's intended to be the opposite
3

One option is to use JSON.

require 'json'
filters = "[\"Hey\", \"ohh\"]"
JSON.parse(filters)

returns:

["Hey","ohh"]

Comments

0

You need to remove extra characters and then split the string into array using the split pattern like this:

"[\"Hey\", \"ohh\"]".gsub(/(\[\"|\"\])/, '').split('", "')

Which returns:

["Hey", "ohh"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.