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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Whoa whoa whoa. form_for is for the VIEW. It generates html. Specifcally, it generates <form> tags and etc... On Nov 26, 2:35 pm, RobL <pgdst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You might wanna consider creating your own form builder instead putting into helpers. Look at formtastic gem as an example. Robert Pankowecki http://robert.pankowecki.pl -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On 27 November 2010 03:16, daze <dmonopoly10-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Whoa whoa whoa. form_for is for the VIEW. It generates html. > Specifcally, it generates <form> tags and etc... > > > On Nov 26, 2:35 pm, RobL <pgdst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Rob Lacey contact-+WAvBcCRUVA@public.gmane.org http://www.robl.me -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 27 November 2010 09:16, Rob Lacey <contact-+WAvBcCRUVA@public.gmane.org> wrote:> 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 > > endI 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> > RobL > > On 27 November 2010 03:16, daze <dmonopoly10-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Whoa whoa whoa. form_for is for the VIEW. It generates html. >> Specifcally, it generates <form> tags and etc... >> >> >> On Nov 26, 2:35 pm, RobL <pgdst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> 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 >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > > > -- > Rob Lacey > contact-+WAvBcCRUVA@public.gmane.org > http://www.robl.me > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.> > RobLBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.