Using form_for inside a helper

Hi there,

I'm having an awful lot of trouble trying to use form_for inside a helper. Any ideas how to get this to work?

module MyHelper

   def button_thing_attempt_1      form_for(Button.new) do |f|       f.submit 'Push'     end   end

  def button_thing_attempt_2     with_output_buffer(form_for(Button.new) do |f|       f.submit 'Push'     end)   end

end

While I get attempt 2 to render the form, the submit button is missing. I'm just not quite sure how to really capture the complete output of form_for.

Many thanks for any help here

RobL

Whoa whoa whoa. form_for is for the VIEW. It generates html. Specifcally, it generates <form> tags and etc...

You might wanna consider creating your own form builder instead putting into helpers. Look at formtastic gem as an example.

Robert Pankowecki

I'm judging from your response that you would you argue that using a Rails view helper inside another view helper is wrong then? I would say this isn't crazy its just trying to DRY up a small form (just a button) I am using in lots of different places.

I found the solution in the end.

module MyHelper

def button_thing_attempt_2    with_output_buffer(form_for(Button.new) do |f|      concat(f.submit 'Push')    end) end

end

RobL

I'm judging from your response that you would you argue that using a Rails view helper inside another view helper is wrong then? I would say this isn't crazy its just trying to DRY up a small form (just a button) I am using in lots of different places.

I found the solution in the end.

module MyHelper

def button_thing_attempt_2 with_output_buffer(form_for(Button.new) do |f| concat(f.submit 'Push') end) end

end

I think it would be more conventional to put the form in a partial to achieve the re-usability.

By the way it is considered bad form (!) to access a model directly from the view or view helper. It is better in the controller to say @button = Button.new then use @button in the view.

Colin

Without opening a debate on the issue. I find the whole bad form, conventions as interesting as it is frustrating. I need to re-use this button helper in many views several times over for several different buttons. Say...

<%= button_helper(:fruit) %> <%= button_helper(:fungi) %> <%= button_helper(:tree) %>

which would require calling initialize on Fungi, Fruit, Tree objects within the helper. If you assigned an instance variable in each controller action for everytime you used the helper you'd actually make re-using the code in many different places more awkward. Not to mention if the number and type of buttons was also dynamic. Conventions are a good building block, but on occasion you may need to stray from them to make life easier.

Do you know of any 'Bad Form vs Good Form' Rails articles, I'd be interested in ready some.

RobL

Please quote when replying.

Rob Lacey wrote in post #964320:

Without opening a debate on the issue. I find the whole bad form, conventions as interesting as it is frustrating. I need to re-use this button helper in many views several times over for several different buttons. Say...

<%= button_helper(:fruit) %> <%= button_helper(:fungi) %> <%= button_helper(:tree) %>

which would require calling initialize on Fungi, Fruit, Tree objects within the helper.

No! Never initialize model objects in the helper or view. The helper and view should *always* get their model objects from the controller.

If you assigned an instance variable in each controller action for everytime you used the helper you'd actually make re-using the code in many different places more awkward.

Then you can use a before_filter.

Not to mention if the number and type of buttons was also dynamic.

Easy. Set that all in the controller. If you're having trouble with a particular case, please post details.

Conventions are a good building block, but on occasion you may need to stray from them to make life easier.

Not until you fully understand how to work with them. And the convention in Rails MVC architecture is that it is *never* appropriate for the model and view/helper to talk to each other. The controller *must* mediate.

Do you know of any 'Bad Form vs Good Form' Rails articles, I'd be interested in ready some.

Look up articles on Rails MVC.

RobL

Best,