I have a method1
which calls other methods depending on params and then returns a json. One of these methods checks if a specific user exists. If the user doesn't exist the method should render a JavaScript alert. At first I got an error that render was called multiple times (which was correct). So I tried adding break
but received an invalid break
error. So I tried return
but then I still get the Render and/or redirect were called multiple times in this action
. How can I break out of method1 when I'm in method2, so that only the render in method2 gets called?
def method1
data = case params["foobar"]
when "case1"
methodxy
...
else
method2
end
render json: data
end
def method2
if user.exists?
return {...}
else
render(
html: "<script>alert('Unknown user!')</script>".html_safe,
layout: 'application'
)
return
end
end