weyus
February 23, 2008, 1:39am
1
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
weyus
February 26, 2008, 1:11am
2
Possibly. I need to recheck why I did this in the first place.
weyus
February 25, 2008, 3:25am
3
Isn’t this exactly what button_to and button_to_function do?
On Feb 22, 7:39 pm, Wes G. [email protected]
weyus
February 27, 2008, 6:03pm
4
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
weyus
February 27, 2008, 7:25pm
5
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:
The caller (a view) must call this using Ruby eval. tags, <%
inline_button(_erbout, …) %>
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.
The parameter in inline_button must be named _erbout for the same
reason as #2 .
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