Ruby 什么时候使用和传递参数给函数?
Ruby When use and pass parameters to functions?
如果请求格式为 html 或 json,我想在身份验证方法中重定向用户,但始终只显示为已请求 json 格式。
我已经将 (html 和 json) 作为参数传递了!
有人知道这是不是传递参数的正确方法?
def authenticate_user!(html,json)
if request.format.html? && current_user.nil?
redirect_to login_url, notice: "Not authorized"
else
request.format.json? && current_user.nil?
redirect_to download_url, notice: "you need download the file first"
end
end
您不需要添加参数。控制器知道格式。
def authenticate_user!
if current_user.nil?
respond_to do |format|
format.html { redirect_to login_url, notice: 'Not authorized' }
format.json { redirect_to download_url, notice: "You need download the file first" }
end
end
end
如果请求格式为 html 或 json,我想在身份验证方法中重定向用户,但始终只显示为已请求 json 格式。
我已经将 (html 和 json) 作为参数传递了! 有人知道这是不是传递参数的正确方法?
def authenticate_user!(html,json)
if request.format.html? && current_user.nil?
redirect_to login_url, notice: "Not authorized"
else
request.format.json? && current_user.nil?
redirect_to download_url, notice: "you need download the file first"
end
end
您不需要添加参数。控制器知道格式。
def authenticate_user!
if current_user.nil?
respond_to do |format|
format.html { redirect_to login_url, notice: 'Not authorized' }
format.json { redirect_to download_url, notice: "You need download the file first" }
end
end
end