Hello, I am updating an object with an AJAX form: view: <% remote_form_for(@sample, :update => "content2") do %> <p> parameter: <br /> <%= select("sample","parameter",Parameter.find(:all).collect {|p| [p.description.gsub(''_'', '' ''),p.id]},{},{:size => 4, :multiple => true}) %> </p> <%= submit_tag "add" %> <% end %> once form is sent, object is saved in def update in my controller: controller: def update @sample = Sample.find(params[:id]) if @sample.update_attributes(params[:sample]) flash[:notice] = ''Sample was successfully updated.'' redirect_to(@sample) else render :action => "edit" end end my problem is that redirect_to(@sample) call the show method via a HTML request, so all my page is refreshed when the view show.html.erb is displayed...which is not what i want as i sent the form through AJAX in order to update the DOM "content2" Is it possible to change redirect_to so that the call of def show is done through xmlhttprequest and only my partial _show.html.erb is displayed (I already have a show.js.rjs)? I tryied to play with format in update but i always get an argument error when request is done via AJAX. def update: respond_to do |format| if @sample.update_attributes(params[:sample]) flash[:notice] = ''Sample was successfully updated.'' format.html {redirect_to(@sample)} format.xml_http_request :get, "show",:id => @sample else render :action => "edit" end May be i am not using correctly the xml_http_request function. thank you for your help. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> format.xml_http_request :get, "show",:id => @sample > May be i am not using correctly the xml_http_request function.You are correct. I don''t know what xml_http_request is actually supposed to do, but this should read something more like: respond_to do |format| format.html { redirect_to(person_list_url) } format.js format.xml { render :xml => @person.to_xml(:include => @company) } end format.xml_http_request { ... } would be looking for a template something like: show.xml_http_request.erb Of course in reality, it just breaks.> respond_to do |format| > if @sample.update_attributes(params[:sample]) > flash[:notice] = ''Sample was successfully updated.'' > format.html {redirect_to(@sample)} > format.xml_http_request :get, "show",:id => @sample > else > render :action => "edit" > > endThis also feels wrong to me. Maybe something more like: if @sample.update_attributes(params[:sample]) flash[:notice] = ''Sample was successfully updated.'' respond_to do |format| format.html {redirect_to(@sample)} format.js end else render :action => "edit" end -- 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 -~----------~----~----~----~------~----~------~--~---
> if @sample.update_attributes(params[:sample]) > flash[:notice] = ''Sample was successfully updated.'' > respond_to do |format| > format.html {redirect_to(@sample)} > format.js > end > else > render :action => "edit" > endOops, I take this back. It should be more like: # PUT /people/1 # PUT /people/1.xml def update @person = Person.find(params[:id]) respond_to do |format| if @person.update_attributes(params[:person]) flash[:notice] = ''Person was successfully updated.'' format.html { redirect_to(@person) } format.js format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @person.errors, :status => :unprocessable_entity } end end end I don''t know where my head was on that previous post. Sorry about that... -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Robert, I indeed did try with format.js. It occurs an error: template is missing, Missing template samples/update.js.erb. as I say i already have a show template and show.js.rjs that is doing everything necessary if def show is called by an AJAX request. that why i would like to call def show via an xmlhttprequest but from def update in the controller ( i am not sure i am really clear) and i think that is what xml_http_request is supposed to do (but i can be wrong as i am new to AJAX and co). On Sep 10, 8:40 am, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > if @sample.update_attributes(params[:sample]) > > flash[:notice] = ''Sample was successfully updated.'' > > respond_to do |format| > > format.html {redirect_to(@sample)} > > format.js > > end > > else > > render :action => "edit" > > end > > Oops, I take this back. It should be more like: > > # PUT /people/1 > # PUT /people/1.xml > def update > @person = Person.find(params[:id]) > > respond_to do |format| > if @person.update_attributes(params[:person]) > flash[:notice] = ''Person was successfully updated.'' > format.html { redirect_to(@person) } > format.js > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @person.errors, :status => > :unprocessable_entity } > end > end > end > > I don''t know where my head was on that previous post. Sorry about > that... > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 10 Sep 2008, at 15:55, da991319 wrote:> > Hi Robert, > > I indeed did try with format.js. It occurs an error: template is > missing, Missing template samples/update.js.erb. > > as I say i already have a show template and show.js.rjs that is doing > everything necessary if def show is called by an AJAX request. that > why i would like to call def show via an xmlhttprequest but from def > update in the controller ( i am not sure i am really clear) and i > think that is what xml_http_request is supposed to do (but i can be > wrong as i am new to AJAX and co).format doesn''t actually help you here: the ajax request is genuinely requesting html. What you can do is if xhr? ... end (xhr? is an alias for xml_http_request?) which can tell if the request was submitted by prototype. Fred> > > On Sep 10, 8:40 am, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >>> if @sample.update_attributes(params[:sample]) >>> flash[:notice] = ''Sample was successfully updated.'' >>> respond_to do |format| >>> format.html {redirect_to(@sample)} >>> format.js >>> end >>> else >>> render :action => "edit" >>> end >> >> Oops, I take this back. It should be more like: >> >> # PUT /people/1 >> # PUT /people/1.xml >> def update >> @person = Person.find(params[:id]) >> >> respond_to do |format| >> if @person.update_attributes(params[:person]) >> flash[:notice] = ''Person was successfully updated.'' >> format.html { redirect_to(@person) } >> format.js >> format.xml { head :ok } >> else >> format.html { render :action => "edit" } >> format.xml { render :xml => @person.errors, :status => >> :unprocessable_entity } >> end >> end >> end >> >> I don''t know where my head was on that previous post. Sorry about >> that... >> -- >> 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-/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 -~----------~----~----~----~------~----~------~--~---
Hi Frederick,>format doesn''t actually help you here: the ajax request is genuinely > requesting html. What you can do is > > if xhr? > ... > endwhat i should write if xhr? is true? something like: render :partial =>show, :id => @sample if i write that the def show is not call, neither show.js.rjs. thanks On Sep 10, 8:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 Sep 2008, at 15:55, da991319 wrote: > > > > > Hi Robert, > > > I indeed did try with format.js. It occurs an error: template is > > missing, Missing template samples/update.js.erb. > > > as I say i already have a show template and show.js.rjs that is doing > > everything necessary if def show is called by an AJAX request. that > > why i would like to call def show via an xmlhttprequest but from def > > update in the controller ( i am not sure i am really clear) and i > > think that is what xml_http_request is supposed to do (but i can be > > wrong as i am new to AJAX and co). > > format doesn''t actually help you here: the ajax request is genuinely > requesting html. What you can do is > > if xhr? > ... > end > (xhr? is an alias for xml_http_request?) > > which can tell if the request was submitted by prototype. > > Fred > > > > > On Sep 10, 8:40 am, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >>> if @sample.update_attributes(params[:sample]) > >>> flash[:notice] = ''Sample was successfully updated.'' > >>> respond_to do |format| > >>> format.html {redirect_to(@sample)} > >>> format.js > >>> end > >>> else > >>> render :action => "edit" > >>> end > > >> Oops, I take this back. It should be more like: > > >> # PUT /people/1 > >> # PUT /people/1.xml > >> def update > >> @person = Person.find(params[:id]) > > >> respond_to do |format| > >> if @person.update_attributes(params[:person]) > >> flash[:notice] = ''Person was successfully updated.'' > >> format.html { redirect_to(@person) } > >> format.js > >> format.xml { head :ok } > >> else > >> format.html { render :action => "edit" } > >> format.xml { render :xml => @person.errors, :status => > >> :unprocessable_entity } > >> end > >> end > >> end > > >> I don''t know where my head was on that previous post. Sorry about > >> that... > >> -- > >> 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---