1

Post and save as array to postgres db.

<% ['auto', 'homeowners', 'health'].map do |service| %>
  <div class="element element-float">
    <%= check_box_tag 'agent[agent_services]', [service] %> <%= service %>
  </div>
<% end %>

Currently submitting the form will only save the last item on the page checked. So if I try to check "auto" and "homeowners" only "homeowners" is saved. Rails log:

"agent"=>{"agent_services"=>"homeowners"}

What I need to do is save all of the checked elements into ROW#agent_services as an array. So if I check "auto" and "homeowners", I need the following to happen:

"agent"=>{"agent_services"=>["auto", "homeowners"]}

I can do this with js but I want to know the rails way. Also the next problem is that postgresql#agent_services == String .. Is there a way to make this row accept an array?

Thanks!

Having an issue saving to postgres column is add_column :agents, :agent_services, :string, array: true

params.require(:agent).permit( . . . , :agent_services => [])

Processing by AgentsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"t/aqjJWwd24czZNXKVS5q9yjf6WU15/artpAaXtb28s=", "agent"=>{"agent_services"=>["auto", "homeowners", "health"]}, "button"=>"", "id"=>"60"}
  Agent Load (0.3ms)  SELECT  "agents".* FROM "agents"  WHERE "agents"."id" = $1 LIMIT 1  [["id", 60]]
   (0.1ms)  BEGIN
  SQL (0.5ms)  UPDATE "agents" SET "agent_services" = $1, "updated_at" = $2 WHERE "agents"."id" = 60  [["agent_services", "{\"auto\",\"homeowners\",\"health\"}"], ["updated_at", "2014-09-17 18:50:52.190149"]]
PG::DatatypeMismatch: ERROR:  column "agent_services" is of type character varying[] but expression is of type character varying at character 40
HINT:  You will need to rewrite or cast the expression.
: UPDATE "agents" SET "agent_services" = $1, "updated_at" = $2 WHERE "agents"."id" = 60
   (0.1ms)  ROLLBACK
MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123
  • Also I suggest you to use a constant as the different check box options in order to keep it defined at one place, then do a double-check on the server side that the values in `agent_services` only includes stuff that is defined in this constant ;) – MrYoshiji Sep 17 '14 at 19:27

1 Answers1

1

You almost got it:

check_box_tag 'agent[agent_services][]', [service]

will provide you params like you wanted:

"agent"=>{"agent_services"=>["auto", "homeowners"]}
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • This is strange.. Getting "Unpermitted parameters: agent_services" but it is in the permit array. Do I need to change something? – MrPizzaFace Sep 17 '14 at 18:36
  • Yes I think you have to declare the `agent_services` as an Array (http://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters) – MrYoshiji Sep 17 '14 at 18:49
  • Yeah do you know about postgresql cast types? Updated the question – MrPizzaFace Sep 17 '14 at 18:53
  • Sorry ment to plus you not accept.. question isn't solved for me quite yet – MrPizzaFace Sep 17 '14 at 18:58
  • This actually concerns another subject and should be asked in another post. But we can try to solve it here. They way Rails is trying to save the `agent_services` attribute's value is as a Ruby Array in the DB. This can be done via serialization of the `agent_services` attribute: `serialize :agent_servers, :array`. – MrYoshiji Sep 17 '14 at 19:00
  • Some how restarting the server fixed the problem... Thanks! – MrPizzaFace Sep 17 '14 at 19:03