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]
[games, fun, sports]and[Hey, ohh]would be an illegal array definition unlessgames,fun,sports,Heyandohhare variables.