How to convert a string to an array using Ruby on Rails

12,649

Solution 1

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

Solution 2

One option is to use JSON.

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

returns:

["Hey","ohh"]
Share:
12,649

Related videos on Youtube

Bitwise
Author by

Bitwise

Updated on September 14, 2022

Comments

  • Bitwise
    Bitwise over 1 year

    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]
    
    • PJP
      PJP over 7 years
      [games, fun, sports] and [Hey, ohh] would be an illegal array definition unless games, fun, sports, Hey and ohh are variables.