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

**URL:** <https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959>\
**Category:** Rails\
**Created:** [February 23, 2008, 1:39am UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959 "2008-02-23T01:39:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 23, 2008, 1:39am UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959/1 "2008-02-23T01:39:43Z")

</div>

All,

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

def inline\_button(name, form\_options = {}, button\_options = {},  
do\_get=false)  
output = form\_tag(form\_options, {:style =\> “display: inline;”,  
:method =\> do\_get ? ‘get’ : ‘post’})  
output \<\< submit\_tag(name, button\_options.merge(:name =\> ‘’))  
output \<\< end\_form\_tag  
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:

def inline\_button(name, form\_options = {}, button\_options = {},  
do\_get=false)  
form\_for(:dummy, :url =\> form\_options, :html =\> {:style =\> “display:  
inline;”, :method =\> do\_get ? ‘get’ : ‘post’}) do  
submit\_tag(name, button\_options.merge(:name =\> ‘’))  
end  
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:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 26, 2008, 1:11am UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959/2 "2008-02-26T01:11:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 25, 2008, 3:25am UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959/3 "2008-02-25T03:25:59Z")

</div>

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

On Feb 22, 7:39 pm, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 27, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959/4 "2008-02-27T18:03:49Z")

</div>

I remember now. There’s no way with button\_to\* to change the styling of  
the form that the button is embedded in.

Hence, “inline\_button”.

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 27, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/how-to-translate-my-inline-form-helper-to-form-for/129959/5 "2008-02-27T19: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)  
form\_for(:dummy, :url =\> form\_options, :html =\> {:style =\> “display:  
inline;”, :method =\> do\_get ? ‘get’ : ‘post’}) do  
concat(submit\_tag(name, button\_options.merge(:name =\> ‘’)), binding)  
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

form\_for() do  
…  
end

instead of

form\_for() do |f|  
…  
end

Wes
