why-el
2013-Jul-02 14:19 UTC
Respond with JS on create-success, otherwise respond with HTML
Hey fellas, Is there a way to selectively respond with a format depending on whether there were errors creating an object or not? My goal is to *have a remote form fall back to the html version of the ''new'' action if there were errors*. In other words, run the create.js.erb if the save succeeds, otherwise render the html ''new'' action. Say I have the following create method: def create @message = Message.new(params[:message]) respond_to do |format| if @message.save format.html { redirect_to messages_path, notice: ''Your message was successfully sent.'' } format.js else render action: "new" # This does not work when I have a remote form. end end end This will work fine (On both remote html and js formats ) if the @message saves, but when I try and do this using Ajax and they were errors, the render call responds with html that gets ignored by the browser (who is expecting js). How can force the browser to render the HTML version of ''new'' even if the original action was JS? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d2a2b8cf-bea6-4cfd-b08f-239ebf4c1a0e%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Mohamed Wael Khobalatte
2013-Jul-02 19:45 UTC
Re: Respond with JS on create-success, otherwise respond with HTML
Any ideas on this? On Tue, Jul 2, 2013 at 3:19 PM, why-el <wael.khobalatte-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey fellas, > > Is there a way to selectively respond with a format depending on whether > there were errors creating an object or not? > > My goal is to *have a remote form fall back to the html version of the > ''new'' action if there were errors*. In other words, run the create.js.erb > if the save succeeds, otherwise render the html ''new'' action. > > Say I have the following create method: > > def create > @message = Message.new(params[:message]) > > respond_to do |format| > if @message.save > format.html { > redirect_to messages_path, notice: ''Your message was successfully > sent.'' > } > format.js > else > render action: "new" # This does not work when I have a remote > form. > end > end > end > > This will work fine (On both remote html and js formats ) if the @message > saves, but when I try and do this using Ajax and they were errors, the > render call responds with html that gets ignored by the browser (who is > expecting js). How can force the browser to render the HTML version of > ''new'' even if the original action was JS? > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/d2a2b8cf-bea6-4cfd-b08f-239ebf4c1a0e%40googlegroups.com > . > For more options, visit https://groups.google.com/groups/opt_out. > > >-- Mohamed Wael Khobalatte -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAEtiScmJJTnVs1jNpPjSsBseC9q-oxTbGXARhU1nZO0GS9%3DU0g%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.