trailblazer / formular

Form builder for Ruby. Fast, Furious, and Framework-Agnostic.
MIT License
81 stars 17 forks source link

Setting input value to nil result in calling undefined method. #39

Closed Titinux closed 7 years ago

Titinux commented 7 years ago

I'm using formular master branch

When value is set to nil, the element try to call a method named after the first parameter. Is this the right behaviour ? I was expecting a tag with value attribute set to an empty string.

var = 'my_value'
f.input(:foo, value: var).to_s

=> <input value="my_value" name="foo" id="foo" type="text">

var = nil
f.input(:foo, value: var).to_s

=> undefined method 'foo'

apotonick commented 7 years ago

Hm, you're right, the value reader shouldn't be called at all if the :value key is present.

The idea behind that is to allow not calling anything when rendering a form, e.g. for "virtual" fields.

Thanks for your bug reports @Titinux, they are very understandable and excellent. :rocket:

apotonick commented 7 years ago

:point_up: @emaglio @fran-worley @trailblazer/website

Should we refrain from calling the value reader on the model if the key :value is present in the options hash?

emaglio commented 7 years ago

I have tried to recreate the issue in gemgem-sinatra but as per @Titinux expectation when value is set to nil I have value: "":

some = nil
= f.input(:title, value: some).to_s

<input value="" name="title" id="title" class="form-control" type="text">

I'm missing something maybe...

Titinux commented 7 years ago

@emaglio Does title a function or a var defined in your example ?

emaglio commented 7 years ago

@Titinux It's a property of a contract: https://github.com/apotonick/gemgem-sinatra/blob/formular-slim-bootstrap3/concepts/post/operation/create.rb#L6

I usually have undefined method when you try to use something not defined in your model/contract

fran-worley commented 7 years ago

@Titinux is right, if an option is set to nil it attempts to call the default method which in this case, was the reader value. I agree that this doesn't make sense as you'd assume that in passing an option, that option would be respected regardless of its value.

I've now changed this & included a couple of test cases to prove it.