开发者

Is there a way to stop execution of calling method to avoid DoubleRenderError in Rails?

开发者 https://www.devze.com 2023-01-23 07:38 出处:网络
In my controller I have: def update case params[:something] when \'x\' if all_is_good good_stuff_happens flash[:notice] = \"Good stuff happened\"

In my controller I have:

def update
  case params[:something]
  when 'x'
    if all_is_good
      good_stuff_happens
      flash[:notice] = "Good stuff happened"
    else
      access_denied
    end
  when 'y'
    other_good_stuff_happens
    flash[:noti开发者_如何学运维ce] = "Other good stuff happened"
  when 'z'
    even_more_good_stuff_happens
    flash[:notice] = "Even more good stuff happened"
  end
  redirect_to good_place_path       
end

And in my ApplicationController I have:

def access_denied
  redirect_to message_path, :alert => 'Access Denied'
end

For the most part, I always want to redirect_to the good_place_path after the case statement.

Occasionally, I want to deny access and leave it at that. Is there a way I can call

acccess_denied

from my controller and then not return to the calling controller (or else it attempts to do a second redirect and thus I get the DoubleRenderError). I understand I could just put

redirect_to good_place_path       

in each when statement, but I'm wondering if there's a more elegant solution.

Thanks.

Sean


Add return after the redirect_to works so adding it after the access_denied should work as well. You basically don't want to fall through to the rendering, which is the default.

0

精彩评论

暂无评论...
验证码 换一张
取 消