# How to translate my inline form helper to form\_for?

**URL:** <https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345>\
**Category:** rubyonrails-talk\
**Created:** [February 23, 2008, 12:39am UTC](https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345 "2008-02-23T00:39:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 23, 2008, 12:39am UTC](https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345/1 "2008-02-23T00:39:43Z")

</div>

All,

In application\_helper.rb, I have the following custom helper to let me generate an inline form with one button:

&nbsp;&nbsp;def inline\_button(name, form\_options = {}, button\_options = {}, do\_get=false) &nbsp;&nbsp;&nbsp;&nbsp;output = form\_tag(form\_options, {:style =\> "display: inline;", :method =\> do\_get ? 'get' : 'post'}) &nbsp;&nbsp;&nbsp;&nbsp;output \<\< submit\_tag(name, button\_options.merge(:name =\> '')) &nbsp;&nbsp;&nbsp;&nbsp;output \<\< end\_form\_tag &nbsp;&nbsp;end

Currently, this works by generating a string which is pulled into the calling view via \<%= inline\_button(...) %\>.

How can I translate this into using form\_for given that the form\_for is now an ERB block and not an interpolated string.

I'm close, and I have this:

&nbsp;&nbsp;def inline\_button(name, form\_options = {}, button\_options = {}, do\_get=false) &nbsp;&nbsp;&nbsp;&nbsp;form\_for(:dummy, :url =\> form\_options, :html =\> {:style =\> "display: inline;", :method =\> do\_get ? 'get' : 'post'}) do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;submit\_tag(name, button\_options.merge(:name =\> '')) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

but not sure what I need to do next.

Or do I need to create a custom form builder instead of trying to put this inside of a helper method?

Thanks, Wes

---

<div class="post-metadata">

**Author:** ![AndyV1](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@AndyV1](https://discuss.rubyonrails.org/u/AndyV1)\
**Post date:** [February 25, 2008, 2:25am UTC](https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345/2 "2008-02-25T02:25:14Z")

</div>

Isn't this exactly what button\_to and button\_to\_function do?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 26, 2008, 12:11am UTC](https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345/3 "2008-02-26T00:11:21Z")

</div>

Possibly. I need to recheck why I did this in the first place.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 27, 2008, 6:25pm UTC](https://discuss.rubyonrails.org/t/how-to-translate-my-inline-form-helper-to-form-for/25345/4 "2008-02-27T18:25:14Z")

</div>

The Answer:

Since form\_for implicitly writes to the "\_erbout" variable via the config method, the inline button method in my helper, which used to generate a string which was included in the view using \<%= inline\_button(...) %\>, now simply takes a passed in output writer (from the view) and manipulates it directly.

Method:

def inline\_button(\_erbout, name, form\_options = {}, button\_options = {}, do\_get=false) &nbsp;&nbsp;form\_for(:dummy, :url =\> form\_options, :html =\> {:style =\> "display: inline;", :method =\> do\_get ? 'get' : 'post'}) do &nbsp;&nbsp;&nbsp;&nbsp;concat(submit\_tag(name, button\_options.merge(:name =\> '')), binding) &nbsp;&nbsp;end end

A couple of things to notice:

1) The caller (a view) must call this using Ruby eval. tags, \<% inline\_button(\_erbout, ...) %\> 2) The caller (a view) must pass the \_erbout variable into the helper, since both form\_for() and concat() expect that variable to be in scope. 3) The parameter in inline\_button \_must\_ be named \_erbout for the same reason as #2. 4) I'm not capturing the yielded form\_builder from the call to form\_for because I don't need it. So it's

&nbsp;&nbsp;form\_for() do &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end

instead of

&nbsp;&nbsp;form\_for() do |f| &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end

Wes
