0

I'm actually posting an Array of ids to my Rails API but the array is received as a hash where the keys are the index of the array.

I already try permitting the parameters params.permit(permission_ids: []) and nothing...

User migration

class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :username, null: false
      t.string :email, null: true
      t.integer :language_id, null: false
      t.integer :permission_ids, array: true, default: []
      t.string :password_digest, null: false
      t.timestamps
    end
    add_index :users, :username, unique: true
  end
end

User params method in the controller.

  def user_params
    params.require(:user).permit(:username,
                                 :email,
                                 :language_id,
                                 :password,
                                 :password_confirmation,
                                 permission_ids: [])
  end

How parameters are received

Parameters: {"user"=>{"username"=>"someusername", "email"=>"someemail@question.com", "language_id"=>"2", "permission_ids"=>{"0"=>"1", "1"=>"2", "2"=>"3", "3"=>"4", "4"=>"5", "5"=>"6", "6"=>"7", "7"=>"8", "8"=>"9", "9"=>"10", "10"=>"11", "11"=>"12"}}}

By the way, I'm posting from Vue using Axios.

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Freissell
  • 79
  • 1
  • 3
  • 2
    I wonder if the real question should be: How to fix the frontend that it sends an array instead? What does the data in the hash actually mean, are the keys or the values the ids in question? – spickermann May 11 '19 at 06:13
  • The frontend is sending an array, the thing is that Rails is parsing it as a hash. The ids are the values of the hash. – Freissell May 11 '19 at 11:15
  • 1
    Can you please show the log entry from such a request? – spickermann May 11 '19 at 11:23
  • @spickermann sorry for the late reply, the request send by Axios is as form url encoded. As the permission_ids param is send like: `permissions_ids[0]: 1, permissions_ids[1]: 2, permissions_ids[2]: 3` – Freissell May 14 '19 at 11:39

2 Answers2

1

The way I manage to get the array of ids:

def user_params
    params[:user][:permission_ids] = (params.dig(:user, :permission_ids) || {}).values
    params.require(:user).permit(:username,
                                 :email,
                                 :language_id,
                                 :password,
                                 :password_confirmation,
                                 permission_ids: [])
  end
Freissell
  • 79
  • 1
  • 3
0

Can you also update the question with html form that is submitting this data. Issue could be in the form as well.

When you want the form to submit an entry as array, the form attribute should be

user['permission_ids'][]

By this, the html form will understand that the input to permission_ids in the html form, will be submitting an array.

For more info: https://stackoverflow.com/a/45233604/4940278

Surya
  • 2,429
  • 1
  • 21
  • 42
  • Yeah, but the thing is that I have a form attach to a JSON in vue.js and the attributes in the request are sent like: `user[permissions_ids][0]: 1, user[permissions_ids][1]: 2, user[permissions_ids][2]: 3`, as you can see are send as an array but I don't why Rails understands that if the param is an Array and has an index you will receive an array of objects... – Freissell May 14 '19 at 11:49