Ruby-on-rails Rails:立即渲染并退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5539106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:51:25  来源:igfitidea点击:

Rails : render and exit immediately

ruby-on-rails

提问by manu_v

Using the omniauth gem, I am forced to define a single route callback for succesful logins, regardless of the provider :

使用 omniauth gem,无论提供者如何,我都被迫为成功登录定义单个路由回调:

def auth_callback 

        auth_data = request.env['omniauth.auth']

        if auth_data.has_key('something')
            process_one(auth_data)
        else
            process_two(auth_data)
        end

        # No view is available here

end


def process_one
    # do something then render view for process_one
    return
end

def process_two
    # do something then render view for process_two
    return
end

How can I prevent the controller from returning to the auth_callback method and try to display the corresponding view (which does not exist) ? Treatment should be considered as complete once the process_one or process_two methods have returned.

如何防止控制器返回 auth_callback 方法并尝试显示相应的视图(不存在)?一旦 process_one 或 process_two 方法返回,处理就应被视为完成。

回答by Kelly

Why not specifically call renderin those methods?

为什么不专门调用render这些方法呢?

def process_one
 # do something then render view for process_one
 render :process_one and return
end

Rails should detect that you've already run it and not try to render again.

Rails 应该检测到您已经运行了它,而不是再次尝试渲染。

回答by Ngoral

If you want to return from the chain of methods, e.g.

如果你想从方法链中返回,例如

def a
  ...
  b
  ...
  render "smth"
end
...
def b
  ...
  # render from some conditional from here
  ...
end

will cause AbstractController::DoubleRenderError, which means that you call rendertwice.

将导致AbstractController::DoubleRenderError,这意味着您调用了render两次。

You can read this articleto find out 4 ways to manage such situation.

您可以阅读这篇文章,找出管理这种情况的 4 种方法。