Hi, It seems there must be a better way to do this than what I am currently doing. I have a simple app that collects a users thoughts along with their email address and their first an last name. On a page that lists all the users thoughts they can click on a link to "add a thought" which then displays a form with ajax. My current methodology for this is not very dry. If js is not working it simply redirects to a "new" page that displays the same form as would have been displayed with ajax. The problem is i have the entire code for this ajaxed form is in the view (rather than the non js "new" page which has the form code in "_form") I have it this way so I can do form_remote_for. I tried modifying "_page" to only have the form setup code, but then it complains about nil objects because i am setting up the form_for or form_remote_for in the view rather than in "_page". Is there some way to DRY this up easily or am I just destined to have my form code in two places. ~Jamie
Hi Jamie, If the form is exactly the same in both cases then you should be able to have the form in only one place. Do you use the rjs templates? Do you know about respond_to? I think these two together can dehydrate your app. Peter On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote:> Hi, > > It seems there must be a better way to do this than what I am > currently doing. > > I have a simple app that collects a users thoughts along with their > email address and their first an last name. On a page that lists all > the users thoughts they can click on a link to "add a thought" which > then displays a form with ajax. My current methodology for this is > not very dry. If js is not working it simply redirects to a "new" > page that displays the same form as would have been displayed with ajax. > > The problem is i have the entire code for this ajaxed form is in the > view (rather than the non js "new" page which has the form code in > "_form") I have it this way so I can do form_remote_for. I tried > modifying "_page" to only have the form setup code, but then it > complains about nil objects because i am setting up the form_for or > form_remote_for in the view rather than in "_page". Is there some way > to DRY this up easily or am I just destined to have my form code in > two places. > > ~Jamie > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''m using RJS templates yes, my problem is i need form_for for the regular requests and form_remote_for for the ajax requests, thus the form is not the same for both cases. ~jamie On Jul 13, 2006, at 6:54 PM, Peter Michaux wrote:> Hi Jamie, > > If the form is exactly the same in both cases then you should be able > to have the form in only one place. Do you use the rjs templates? Do > you know about respond_to? I think these two together can dehydrate > your app. > > Peter > > > On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote: >> Hi, >> >> It seems there must be a better way to do this than what I am >> currently doing. >> >> I have a simple app that collects a users thoughts along with their >> email address and their first an last name. On a page that lists all >> the users thoughts they can click on a link to "add a thought" which >> then displays a form with ajax. My current methodology for this is >> not very dry. If js is not working it simply redirects to a "new" >> page that displays the same form as would have been displayed with >> ajax. >> >> The problem is i have the entire code for this ajaxed form is in the >> view (rather than the non js "new" page which has the form code in >> "_form") I have it this way so I can do form_remote_for. I tried >> modifying "_page" to only have the form setup code, but then it >> complains about nil objects because i am setting up the form_for or >> form_remote_for in the view rather than in "_page". Is there some way >> to DRY this up easily or am I just destined to have my form code in >> two places. >> >> ~Jamie >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Can you post a very simplified stripped down version of your wet code? On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote:> I''m using RJS templates yes, > > my problem is i need form_for for the regular requests and > form_remote_for for the ajax requests, thus the form is not the same > for both cases. > > ~jamie > > On Jul 13, 2006, at 6:54 PM, Peter Michaux wrote: > > > Hi Jamie, > > > > If the form is exactly the same in both cases then you should be able > > to have the form in only one place. Do you use the rjs templates? Do > > you know about respond_to? I think these two together can dehydrate > > your app. > > > > Peter > > > > > > On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote: > >> Hi, > >> > >> It seems there must be a better way to do this than what I am > >> currently doing. > >> > >> I have a simple app that collects a users thoughts along with their > >> email address and their first an last name. On a page that lists all > >> the users thoughts they can click on a link to "add a thought" which > >> then displays a form with ajax. My current methodology for this is > >> not very dry. If js is not working it simply redirects to a "new" > >> page that displays the same form as would have been displayed with > >> ajax. > >> > >> The problem is i have the entire code for this ajaxed form is in the > >> view (rather than the non js "new" page which has the form code in > >> "_form") I have it this way so I can do form_remote_for. I tried > >> modifying "_page" to only have the form setup code, but then it > >> complains about nil objects because i am setting up the form_for or > >> form_remote_for in the view rather than in "_page". Is there some way > >> to DRY this up easily or am I just destined to have my form code in > >> two places. > >> > >> ~Jamie > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Sure... in list.rhtml <h3><%= link_to_remote ''Add A Signature'', :url => { :action => ''show_signature_form'' } %></h3> <div id=''signature_error''><%= error_messages_for :signature %></div> <div id = "signature_form" style="display:none;"> <% remote_form_for :signature, @signature, :url => {:action => ''create''} do |form| %> <p><label for="signature_email">Email:</label><br/> <%= form.text_field :email %></p> .....more form setup here.... <%= submit_tag "Create" %> <%= link_to_remote ''Cancel'', :url => {:action => ''hide_signature_form''}%> <% end %> </div> in new.rhtml (this file exists for javascript degredation purposes) <div id = "signature_form"> <% form_for :signature, @signature, :url => {:action => ''create''} do |form| %> <p><label for="signature_email">Email:</label><br/> <%= form.text_field :email %></p> ...more form setup here... <%= submit_tag "Create" %> <% end %> </div> What I was attempting to say (may not have expressed it clearly) is that i tried to abstract everything between "form_for" and "end" into a partial, however this doesn''t work because when the code is compiled it whines about nil stuff when it hits form.text_field. Notice the remote_form_for in the first one and just form_for in the second one. This is where the problem lies, if i could just use one or the other i could throw the whole mess in a partial. ~Jamie On Jul 13, 2006, at 7:46 PM, Peter Michaux wrote:> Can you post a very simplified stripped down version of your wet code? > > > > On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote: >> I''m using RJS templates yes, >> >> my problem is i need form_for for the regular requests and >> form_remote_for for the ajax requests, thus the form is not the same >> for both cases. >> ~jamie >> >> On Jul 13, 2006, at 6:54 PM, Peter Michaux wrote: >> >> > Hi Jamie, >> > >> > If the form is exactly the same in both cases then you should be >> able >> > to have the form in only one place. Do you use the rjs >> templates? Do >> > you know about respond_to? I think these two together can dehydrate >> > your app. >> > >> > Peter >> > >> > >> > On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote: >> >> Hi, >> >> >> >> It seems there must be a better way to do this than what I am >> >> currently doing. >> >> >> >> I have a simple app that collects a users thoughts along with >> their >> >> email address and their first an last name. On a page that >> lists all >> >> the users thoughts they can click on a link to "add a thought" >> which >> >> then displays a form with ajax. My current methodology for this is >> >> not very dry. If js is not working it simply redirects to a "new" >> >> page that displays the same form as would have been displayed with >> >> ajax. >> >> >> >> The problem is i have the entire code for this ajaxed form is >> in the >> >> view (rather than the non js "new" page which has the form code in >> >> "_form") I have it this way so I can do form_remote_for. I tried >> >> modifying "_page" to only have the form setup code, but then it >> >> complains about nil objects because i am setting up the >> form_for or >> >> form_remote_for in the view rather than in "_page". Is there >> some way >> >> to DRY this up easily or am I just destined to have my form >> code in >> >> two places. >> >> >> >> ~Jamie >> >> _______________________________________________ >> >> Rails mailing list >> >> Rails@lists.rubyonrails.org >> >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > _______________________________________________ >> > Rails mailing list >> > Rails@lists.rubyonrails.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Jamie, Jamie Quint wrote:> my problem is i need form_for for the regular requests and form_remote_for > for the ajax requestsDon''t mean to seem like a smart ass, but... Why? You can use an RJS template pretty much anywhere you can use an .rhtml. What have you tried WRT using "form_remote_for" for the "regular" requests? Best regards, Bill
Well I guess I should have thought of that, I assumed that it would call create.rjs when in fact I wanted new.rhtml, however the proper controller method handled that fact, and now I have DRYed off. Thanks! On Jul 13, 2006, at 8:29 PM, Bill Walton wrote:> Hi Jamie, > > Jamie Quint wrote: > >> my problem is i need form_for for the regular requests and >> form_remote_for for the ajax requests > > Don''t mean to seem like a smart ass, but... > > Why? > > You can use an RJS template pretty much anywhere you can use > an .rhtml. What have you tried WRT using "form_remote_for" for the > "regular" requests? > > Best regards, > Bill > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote:> What I was attempting to say (may not have expressed it clearly) is > that i tried to abstract everything between "form_for" and "end" into > a partial, however this doesn''t work because when the code is > compiled it whines about nil stuff when it hits form.text_field. > > Notice the remote_form_for in the first one and just form_for in the > second one. This is where the problem lies, if i could just use one > or the other i could throw the whole mess in a partial.You know that remote_form_for can take an :html option to specify a "fallback" url for non-JavaScript browsers? http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000414 I''ve been specifying this fallback url for a situation I think is similar to yours. Peter
What I ended up doing was using remote_form_for inside a partial called "_form" <% remote_form_for :signature, @signature, :url => {:action => ''create''} do |form| %> ..... form stuff.... <%= submit_tag "Create" %> <% end %> Then in the controller doing this... def create @signature = Signature.new(params[:signature]) return if request.xhr? if @signature.save flash[:notice] = ''Signature was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end so if it is a rjs request it returns and goes to create.rjs otherwise...well you can see. voila! one controller action to handle both situations, nice and DRY. ~Jamie On Jul 13, 2006, at 9:24 PM, Peter Michaux wrote:> On 7/13/06, Jamie Quint <jsquintz@gmail.com> wrote: > >> What I was attempting to say (may not have expressed it clearly) is >> that i tried to abstract everything between "form_for" and "end" into >> a partial, however this doesn''t work because when the code is >> compiled it whines about nil stuff when it hits form.text_field. >> >> Notice the remote_form_for in the first one and just form_for in the >> second one. This is where the problem lies, if i could just use one >> or the other i could throw the whole mess in a partial. > > You know that remote_form_for can take an :html option to specify a > "fallback" url for non-JavaScript browsers? > > http://api.rubyonrails.com/classes/ActionView/Helpers/ > PrototypeHelper.html#M000414 > > I''ve been specifying this fallback url for a situation I think is > similar to yours. > > Peter > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jamie, you are a genius! I was having the same problem, after following the example from O''Reilly RJS pdf. The example they gave was for creating a new object, and does not work for editing an existing object, but the solution you provided works perfectly, thanks again! Jamie Quint wrote:> What I ended up doing was using remote_form_for inside a partial > called "_form" > > <% remote_form_for :signature, @signature, :url => {:action => > ''create''} do |form| %> > ..... form stuff.... > <%= submit_tag "Create" %> > <% end %> > > Then in the controller doing this... > > def create > @signature = Signature.new(params[:signature]) > return if request.xhr? > if @signature.save > flash[:notice] = ''Signature was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > so if it is a rjs request it returns and goes to create.rjs > otherwise...well you can see. > > voila! one controller action to handle both situations, nice and DRY. > > ~Jamie-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---