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

Setting default values #34

Closed
nysthee opened this issue Aug 24, 2016 · 2 comments
Closed

Setting default values #34

nysthee opened this issue Aug 24, 2016 · 2 comments

Comments

@nysthee
Copy link

nysthee commented Aug 24, 2016

Hey

I'm trying to set default values in a form the virtus way. But it looks like this is not working. Am I overlooking something?

    attribute :warranty, Float, default: 50
@andypike
Copy link
Owner

andypike commented Aug 24, 2016

That should work fine unless you are populating the form with .from_params or .from_model as the values will be overwritten by the values from the params or model.

I've just tested this and it works for me:

class MyForm < Rectify::Form
  attribute :warranty, Float, default: 50
end

form = MyForm.new
form.warranty #=> 50.0

I typically use .new in the new action of a controller and .from_model in the edit action. Then use .from_params in both the create and update actions as you want to populate the form from the values entered by the user.

Does that help at all?

Andy

@nysthee
Copy link
Author

nysthee commented Aug 24, 2016

That is indeed the issue. Thanks for the quick response!

@nysthee nysthee closed this as completed Aug 24, 2016
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

No branches or pull requests

2 participants