How does one generate a form within a view using javascript? Specifically, assume views/premise/new.html.erb looks like this: <%= form_for(Premise.new) do |f| %> <%= f.hidden_field :full_address, :value => $FULL_ADDRESS %> <%= f.hidden_field :geocoding, :value => $GEOCODING %> <%= f.submit :value => "use this address" %> <%= f.label :formatted_address, $FULL_ADDRESS %> <% end %> ... but with the twist that this form must be generated on the client side by javascript. (In this case, $GEOCODING and $FORMATTED_ADDRESS are placeholders for values generated by the Google geocoding service, which requires client-side javascript.) One approach would be to look at the HTML generated by the above .erb code and write javascript functions that emit HTML directly. But this approach reeks -- there must be a better way! Assuming there is a better way, where do I look for documentation and examples? -- 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.
Frederick Cheung
2010-Nov-10 00:13 UTC
Re: help getting started with javascript generated forms
On Nov 9, 11:11 pm, Fearless Fool <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> How does one generate a form within a view using javascript? > > Specifically, assume views/premise/new.html.erb looks like this: > > <%= form_for(Premise.new) do |f| %> > <%= f.hidden_field :full_address, :value => $FULL_ADDRESS %> > <%= f.hidden_field :geocoding, :value => $GEOCODING %> > <%= f.submit :value => "use this address" %> > <%= f.label :formatted_address, $FULL_ADDRESS %> > <% end %> > > ... but with the twist that this form must be generated on the client > side by javascript. (In this case, $GEOCODING and $FORMATTED_ADDRESS > are placeholders for values generated by the Google geocoding service, > which requires client-side javascript.) > > One approach would be to look at the HTML generated by the above .erb > code and write javascript functions that emit HTML directly. But this > approach reeks -- there must be a better way! >Another would be to have the form created server side (ie normally), but fill in the form values client side once they become known (and if you care you can have the form hidden initially and show it only once the geocoding data is available) Fred> Assuming there is a better way, where do I look for documentation and > examples? > > -- > Posted viahttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2010-Nov-10 06:18 UTC
Re: help getting started with javascript generated forms
Frederick Cheung wrote in post #960432:> Another [approach] would be to have the form created server side (ie normally), > but fill in the form values client side once they become known (and if > you care you can have the form hidden initially and show it only once > the geocoding data is available) > > FredWhen I first read this, I slapped my head and said "doh! -- it''s javascript, so of course it can modify the form generated by the server". But then I realized that it won''t exactly work in my case. I oversimplified the original problem statement: the javascript can generate an arbitrary number of these forms, and I don''t know how many that will be beforehand. I s''pose I could let the server generate a single form, as Fred suggests, and use that as a template for the javascript code to replicate/modify as needed. This approach is still a little bizarre but might be easier to maintain. Right now, I''m doing it the stupid way (described in the OP) and it''s working. - ff -- 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.
Marnen Laibow-Koser
2010-Nov-10 16:57 UTC
Re: help getting started with javascript generated forms
Fearless Fool wrote in post #960459:> Frederick Cheung wrote in post #960432: >> Another [approach] would be to have the form created server side (ie normally), >> but fill in the form values client side once they become known (and if >> you care you can have the form hidden initially and show it only once >> the geocoding data is available) >> >> Fred > > When I first read this, I slapped my head and said "doh! -- it''s > javascript, so of course it can modify the form generated by the > server". > > But then I realized that it won''t exactly work in my case. I > oversimplified the original problem statement: the javascript can > generate an arbitrary number of these forms, and I don''t know how many > that will be beforehand.In that case, you may want to use the Ajax approach. Instead of having the JS generate the form itself, have it request a rendered partial from the server. That way you can still use ERb (or better yet, Haml) rather than building the form in JavaScript, but the rendered forms don''t all have to go into the DOM in advance. Best, -- 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.