0

I am trying to trigger the url where I am passing parameters in the array format

http://localhost:3000/abc/23?field_selectors=['surname', 'firstname']

when I try to get this params it comes in string like

[2] pry(#<PersonController>)> params[:field_selectors] => "['surname', 'firstname']"

I tried to remove the "" from both the ends but it again return a string.

"['surname', 'firstname']".chomp('"').reverse.chomp('"').reverse

The above implementation is very idiotic but I tried

Now I am confused how can I covert the given string into an array or how can i get the params directly as an array.

8
  • have you tried params[:field_selectors].to_a ? Commented Jul 20, 2018 at 13:36
  • yup @ThorTL67, but it didnt work Commented Jul 20, 2018 at 13:37
  • 1
    Isn't it better to pass string to params first like 'surname,firstname ' and then convert it into array that you wanted to? Commented Jul 20, 2018 at 13:38
  • 6
    try http://localhost:3000/abc/23?field_selectors[]=surname&field_selectors[]=firstname Commented Jul 20, 2018 at 13:46
  • 1
    @HardikUpadhyay, Perfect man, just what I am looking for ...:-) Commented Jul 20, 2018 at 14:03

1 Answer 1

1

If you want params to register as an array to Rails' param parsing code, append [] to the end of the param:

http://localhost:3000/abc/23?field_selectors[]=surname&field_selectors[]=firstname

params[:field_selectors] will then automatically be parsed as an array of strings.

Sign up to request clarification or add additional context in comments.

Comments

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.