views:

191

answers:

1

How do you build a helper method that looks like

-confirmation_for [@post, @comment] do |f|
  = f.confirm "Post"
  %p html here...
  = f.edit    "Edit"

and encapsulates two forms like

-form_for [@post, @commment] do |f|
  = f.hidden_field :submission_state, :value => "confirmed"
  = f.submit "Post"
%p html here...
-form_for [@post, @commment] do |f|
  = f.hidden_field :submission_state, :value => "edit_requested"
  = f.submit "Edit"
A: 

I don't think there's a way to get exactly what you're looking for without redefining form_for. Or writing your own version of it.

You can get close with FormBuilders or Partials or Helpers, but none of these concepts either alone will let you do what you're looking to do.

A FormBuilder will let you define methods to be invoked on the form, like confirm and edit, but they would be part of the same form. Unless you create two forms.

In the related helper file:

class ExampleFormBuilder < ActionView::Helpers::FormBuilder    

  def confirm
    hidden_field(:submission_state, :value => "confirmed") + "\n" + submit "Post"
  end

  def edit
    hidden_field(:submission_state, :value => "edit_requested") + "\n" + submit "Edit"
  end


end

Sample usage:

- form_for [@posts,@comments], :builder => ExampleFormBuilder do |f|
  = f.confirm
- form_for [@posts,@comments], :builder => ExampleFormBuilder do |f|
  = f.edit

When used with a partial you could do something like this

partial:

- form_for [@posts,@comments], :builder => ExampleFormBuilder do |f|
  = f.send(action)

view:

= render :partial => :confirmation_for, :locals => {:action => :confirm}
= render :partial => :confirmation_for, :locals => {:action => :edit}

You could then bundle both partial calls into another partial. But that's going a little too far.

EmFi