Array parameters

796 views
Skip to first unread message

Damon Silver

unread,
Mar 11, 2014, 12:47:27 AM3/11/14
to ruby-...@googlegroups.com
I'm attempting to use array parameters a la http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters, e.g., ?foo[]=bar&foo[]=baz, but can't seem to get Grape to play nicely with this.  Does Grape support this, and if so, what is the syntax to do so?  

The only reference I've found is http://stackoverflow.com/questions/18314017/testing-rails-grape-api-with-curl-params-array, but that doesn't demonstrate how to actually enable the desired behavior.

Thanks in advance,

Damon Silver

Daniel Doubrovkine

unread,
Mar 11, 2014, 5:35:54 PM3/11/14
to ruby-...@googlegroups.com
Grape definitely supports this.

I added this to a simple API:

    get '/array' do
      p params[:ary]
    end


this gave me an array with ["x", "y"]


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Damon Silver

unread,
Mar 11, 2014, 5:53:47 PM3/11/14
to ruby-...@googlegroups.com
How do you declare the parameters?  I was trying something like this and seeing errors:

params do
  requires 'foo[]', type: String, desc: 'something about foo'
end

If I simply declare the parameter as "requires :foo, ...", will Grape do the translation to an array of strings if I submit a query with multiple foo parameters?  That's what your example above seems to suggest.

Thanks.




--
You received this message because you are subscribed to a topic in the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-grape/bbfKRhLmSdk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-grape+...@googlegroups.com.

Daniel Doubrovkine

unread,
Mar 11, 2014, 6:00:25 PM3/11/14
to ruby-...@googlegroups.com
If `foo` is supposed to be an array, you should declare it as such, requires :foo, type: Array. Does that work?

Damon Silver

unread,
Mar 11, 2014, 6:06:40 PM3/11/14
to ruby-...@googlegroups.com
Working now.  Thanks for the help!

Damon Silver

unread,
Mar 11, 2014, 6:18:11 PM3/11/14
to ruby-...@googlegroups.com
P.S. When possible, it'd be nice to update the documentation to list the acceptable parameter types and provide examples for each.

Daniel Doubrovkine

unread,
Mar 11, 2014, 7:07:06 PM3/11/14
to ruby-...@googlegroups.com
Please do update the documentation! See https://github.com/intridea/grape/blob/master/CONTRIBUTING.md.
Reply all
Reply to author
Forward
0 new messages