

Build complex forms in rails with FormBuilder  - pothibo
http://pothibo.com/2014/04/build-your-forms-with-formbuilder/

======
jahkeup
I think there are many benefits in decoupling this form presentation logic vs
plain view further with form builder compared to some gems. The ability to
work with the form, create your own fields, and use presentation logic outside
of the form view is great. And now knowing it's built in to rails, this is
fantastic (and it's also good to know I don't always have to go to other form
builder gems for their conciseness when I want that _and_ control).

~~~
antjanus
Awesome. I usually don't see anyone understanding the necessity of dynamic
form building.

In this case, you could easily get a form declaration from a database or
another file, go through your controller (or form builder class) to translate
it and add the correct parameters.

For those that don't get why you'd need to do that, here are some examples:

* creating forms in multiple languages (the form controller can take care of content/labeling/placeholders) * creating forms to comply in multiple countries (for example, the same sign up form in Europe may require a "accept cookies when I'm on site" checkbox or something similar). * creating forms based on preselected options. For instance, if a user is signing up for a free account, they won't need to give their name. While a paid account (by necessity of billing) will require credit card info.

Really cool :)

------
sergiotapia
Just use simple_form my friend!

    
    
        = simple_form_for @user, html: { class: 'form-horizontal' } do |f|
          = f.error_notification
      
          .form-inputs
            = f.input :username, label: "Enter your username:", required: true
            = f.input :password, label: "Enter your password.", required: true
    
          .form-actions
            = f.button :submit, "Save Changes", class: 'btn btn-primary'
    

Note though that this is a huge benefit of using simple_form _in addition_ to
Slim.

~~~
pothibo
I really wanted to avoid going in a discussion between gems trying to replace
FormBuilder but I feel there needs to be some clarification.

I __have __used simple_form, and I have used many different gems in the past.
Simply saying that using simple_form solves every issues completely misses the
point.

There's a lot of forms out there that are built dynamically that needs a lot
of conditions and evaluations to build the gems. CSS class can be
added/removed depending on the current status of the user. This kind of things
CANNOT be easily done with simple_form.

For example, what if I need to add a class to the form if the user is an
admin? Would you create a helper method for that? Or a ternary condition?

If I mentioned simple_form in the very beginning of the article, it's possible
that I did it so I would avoid this kind of facile counter-argument. I guess I
was wrong.

~~~
midas007
It's a toolbox, each gem has unique strengths and weaknesses.

If using ActiveAdmin, it's likely that large projects that include external
apps already require simpleform and/or formtastic in addition to
form_for/formbuilder.

Whatever works best for a given situation / is easiest / most maintainable
gets my vote.

------
snitko
I cannot so any clear advantage over simple_form here.

~~~
romanovcode
Only seems more complicated and a total overkill. Reminds me of Zend Framework
(php) where you would need to write 60 loc to add a simple form with two
fields.

~~~
matthucke
As a Zend Framework refugee, I agree. View stuff belongs in the view.

Additionally, for those of us who works with designers, this gets in the way.
My designer-colleague is comfortable editing HAML by now; I want to give him
the templates and let him move things around without having to involve me for
things like where a label should be.

------
jlsync
nice post, which demystified FormBuilder a bit for me. (yes, simple_form is
awesome, but the minute you try to do something nonstandard (e.g. bootstrap3
compatibility) you have to fight with it very hard and it becomes just another
leaky abstraction).

------
yeabuddy
Why re-invent simple_form with more code that you'll have to maintain now?

