0

I'm trying to convert a string to an array. The method I'm using seems to be working but when I check with pry it's still a string. Let me show you my method and output and see if you can tell me what's wrong. Thank you.

Method:

  def filters=(filters)
    if filters.is_a?(String)
      filters = filters.split(",")
    end

    list = Set.new

    Array.wrap(filters).each do |filter|
      filter.tr!(",", " ")
      filter.strip!
      list << filter unless filter == ""
    end

    super(list.to_a)
  end

View

in the text field I placed hey, ohh

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

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

if you put a pry on the end of that it seems that it has converted to an array. but when I check the object it still remains a string.

when I look at my filters now in the console they look like this

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

when I want it to look like:

["Hey", "ohh"]

4
  • 1
    can you give me example input/oupt what you are trying to achieve ? Commented Sep 5, 2016 at 16:58
  • can you publish something runnable ? the class with the meaningfull methods Commented Sep 5, 2016 at 18:45
  • Possible duplicate of How to convert a string to an array using Ruby on Rails Commented Sep 6, 2016 at 0:47
  • you checked when method is executed? Commented Jul 10, 2017 at 8:04

0

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.