Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default value for the optional param #390

Merged
merged 4 commits into from
Apr 29, 2013
Merged

Conversation

oivoodoo
Copy link
Contributor

Hello.

Probably it's not a good way to specify default param using validators. But I haven't found another way for now.

What do you think about it?

optional :type1, default: 'default-value1'

Test Examples:

get '/' { params[:type1] }

get '/'
last_response.body.should == 'default-value1'

get '/?type1=passed-value'
last_response.body.should == 'passed-value'

@dblock
Copy link
Member

dblock commented Apr 24, 2013

Semantically, it seems a little strange that a parameter is optional, but has a default value. Let me meditate on this for a bit. Appreciate any further thoughts on this.

Please do update CHANGELOG as well, thank you.

@oivoodoo
Copy link
Contributor Author

@dblock I've just updated changelog. Thanks for reviewing. For now I am using this approach for defining default values for the api.

Example:

params do
  optional :connection_type, type: String, default: 'all', desc: 'wifi, carrier or all'
end
get '/' do
  devices = Device.where(connection_type: params[:connection_type]) # if developer doesn't pass this value I am taking devices with all connection type.
end

@dblock
Copy link
Member

dblock commented Apr 29, 2013

Merging, thank you.

@dblock dblock merged commit bd26c19 into ruby-grape:master Apr 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants