JB2
JB2

Reputation: 1607

Understanding Rails URL parameters

Apologies for the basic question, however I am completely new to Rails and am trying to debug an existing application.

Specifically, I am trying to reverse-engineer the URL, given the following controller:

def find_by_foo
    params.require(:foo).permit(:bar1, :bar2)
end

The route is as follows:

get 'find_by_foo', on: :collection

And I even have a spec:

it "should not be successful" do
    get :find_by_foo, params: {foo: BarStuff}
    expect(response).to_not be_success
    expect(response).to have_http_status(401)
 end

I am trying to figure out how to pass these parameters via my REST client. I have tried the following URL structures:

api/v1/collections/find_by_foo?foo={bar1: 0, bar2: 0}
api/v1/collections/find_by_foo/foo/0/0
api/v1/collections/find_by_foo?params={foo: {bar1: 0, bar2: 0}}

All to now avail. What is the correct structure here?

Upvotes: 0

Views: 66

Answers (2)

Andrew Schwartz
Andrew Schwartz

Reputation: 4657

Do you mean like this?

api/v1/collections/find_by_foo?foo[bar1]=0&foo[bar2]=0

An overview of this is available in the ActionController Overview

You can also, in the console, call

app.find_by_foo_api_v1_collections_path(foo:{ bar1: 0, bar2: 0})

(or whatever the actual url helper method is, I'm just guessing based on the route), and you'll get the CGI-escaped version of the path that you can use. You can wrap that in CGI.unescape(...) for easier reading. E.g., in my app:

2.2.2 > CGI.unescape(app.edit_many_ops_media_files_path(foo:{ bar1: 0, bar2: 0}))
# => "/files/edit_many?foo[bar1]=0&foo[bar2]=0"

Upvotes: 3

Juanito Fatas
Juanito Fatas

Reputation: 9949

You can print out the routes and filter by find_by_foo to see the correct structure. Run this in terminal:

$ rake routes | grep find_by_foo

Hope this helps.

Upvotes: 0

Related Questions