Reputation: 1101
I am looking to generate a route in Phoenix that will accept 2 query params.
get "/items?id=:id&action=:action", ActionController, :index_by
But I get the following error:
(Plug.Router.InvalidSpecError) :identifier in routes must be made of letters, numbers and underscores
I noticed that when I remove the second parameter that it compiles just fine, so I am guessing this has something to do with the delimiter &
to separate out the params in the query string.
Is there another way to specify multiple params like that to differentiate the route?
Upvotes: 2
Views: 3793
Reputation: 1082
Route definitions are mainly for 'clean urls' since it matches on the request path (without the query string).
With that in mind, you can define your route like this
get("item/:id/:action", ActionController, :index_by)
#Or
get("/items", ActionController, :index_by)
The first route definition will capture id
and action
from the request path e.g GET /items/1/edit will give you %{"id"=>1, "action"=>"edit"}
in your params.
The second will capture id
and action
from the query string. e.g GET "/items?id=1&action=delete"
will give you %{"id"=>1, "action"=>"delete"}
in your params
Note that, the second route definition, unlike the first, does not enforce the presence of either id
or action
in the query string so you are not guaranteed those paramaters will be available in your params.
Upvotes: 6