简体   繁体   中英

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?

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. Eg, 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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM