andypike / rectify

Build maintainable Rails apps
MIT License
596 stars 48 forks source link

Setting default values #34

Closed nysthee closed 8 years ago

nysthee commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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