Hi Everyone, (using rails 3.2.11) I''ve searched high and low for the answer to this, so now I have to submit a question... # describe "POST on Users#create" do> # before { post users_path } > # specify { response.should redirect_to(root_path) } > # endEvery time I run rspec with that test, I get this: Failure/Error: before { post users_path }> AbstractController::DoubleRenderError: > Render and/or redirect were called multiple times in this action. > Please note that you may only call render OR redirect, and at most once per > action. Also note that neither redirect nor render terminate execution of > the action, so if you want to exit an action after redirecting, you need to > do something like "redirect_to(...) and return".What am I doing wrong? Thank you. -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/TfxFT6X_GLgJ. For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Jan 13, 2013 at 5:23 AM, Peter <peter-I8Khkwz7QpbQT0dZR+AlfA@public.gmane.org> wrote:> I''ve searched high and low for the answer to this, so now I have to submit a > question... > >> # describe "POST on Users#create" do >> # before { post users_path } >> # specify { response.should redirect_to(root_path) } >> # end > > > Every time I run rspec with that test, I get this: > >> Failure/Error: before { post users_path } >> AbstractController::DoubleRenderError: >> Render and/or redirect were called multiple times in this action. >> Please note that you may only call render OR redirect, and at most once per >> action. Also note that neither redirect nor render terminate execution of >> the action, so if you want to exit an action after redirecting, you need to >> do something like "redirect_to(...) and return". > > > What am I doing wrong? Thank you.Renders and redirects in Rails do not trigger a halt on the http stack so you need to trigger the halt yourself inside of the action by using "return render :blah" in your if or "return redirect_to :back". What I am saying is if you have a conditional and you render and then have another render it will trigger a double render because it does not halt the http and render or redirect, it continues to let you finish your job if you need to... such as closing off connections and what-have-you. An example: MyController < ApplicationController def my_action redirect_to :back unless params["blah"] == "blah" render :my_template end end Should be: MyController < ApplicationController def my_action return redirect_to :back unless params["blah"] == "blah" render :my_template end end -- 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 https://groups.google.com/groups/opt_out.
On Sun, Jan 13, 2013 at 9:17 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> MyController < ApplicationController > def my_action > redirect_to :back unless params["blah"] == "blah" > render :my_template > end > end > > Should be: > > MyController < ApplicationController > def my_action > return redirect_to :back unless params["blah"] == "blah" > render :my_template > end > endTo add before somebody points it out, I would never personally write a simple method like that, I would instead prefer to do something like: MyController < ApplicationController def my_action prams["blah"] == "blah" ? render(:my_template) : redirect_to(:back) end end -- 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 https://groups.google.com/groups/opt_out.