Nick
Nick

Reputation: 29

Rails routing forcing param to be present

I'm fairly new to ruby api development and have created the below endpoint in my routes.rb

get "/users/active_users/:since"

However, when the param is not given, I want the param to default to a certain value. How do I enforce this? Also, I want to enforce that param be an integer and not alpha/alpha-numeric. Help is appreciated!

Upvotes: 0

Views: 288

Answers (1)

Anand
Anand

Reputation: 6531

get "/users/active_users/:since"

By making this routes its compulsory to give the params[:since] , other wise it will throw back a error not routes matches

So here by i would suggest you to make routes

get "/users/active_users"

Since it's get type request so its won't affect more, you can append params[:since] in query with routes like this:-

/users/active_users?since=1999

And at your controller you will get params[:since] = 1999

So far as i know it can't be enforce routes to accept only integer params but it can be handle at controller side

params[:since].is_a? Integer
=> true

Or

params[:since].to_i

Upvotes: 1

Related Questions