0

I have this in my template:

<%= f.hidden_field :user_ids, multiple: true, value: 1 %>
<%= f.hidden_field :user_ids, multiple: true, value: 2 %>

On the controllerI have this to make sure that the array is an array of integers. I tested it and it works fine :

before_filter :parse_json_request

  def parse_json_request
    params[:keepcon_settings_users_group][:user_ids] = params[:keepcon_settings_users_group][:user_ids].map(&:to_i) if params[:keepcon_settings_users_group][:user_ids]
  end

The problem is that the data is storing as a yaml but in this format:

---
- '1'
- '2'

And I need it this way:

---
- 1
- 2

My class is:

class UsersGroup < ActiveRecord::Base
  belongs_to :account_setting
  serialize :user_ids, Array
  attr_accessible :name, :user_ids
  after_initialize :set_defaults

  def set_defaults
    if !self.user_ids or self.user_ids == ''
      self.user_ids = []
    end
  end

end

1 Answer 1

3

I think the incoming data from a HTML form is always sent as a string. You can fix it by calling to_i on each of the params like so:

user_ids.map!(&:to_i)
Sign up to request clarification or add additional context in comments.

5 Comments

Im doing that here: params[:keepcon_settings_users_group][:user_ids].map(&:to_i)
Oops - didn't see that with the long line! is the conversion working? You could try puts params[:keepcon_settings_users_group][:user_ids].first.class.name on the next line
Where is the YAML save happening?
The problem was the order in which I used the before filter! Thanxs Matt
Form hands me this ""1,2" Applied this method and got: undefined method `map!' for "1,2":String

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.