How to translate my inline form helper to form_for?

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

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

Isn’t this exactly what button_to and button_to_function do?

On Feb 22, 7:39 pm, Wes G. [email protected]

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

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