我有一个 Controller 操作,只能由登录的用户访问。如果用户未登录,我想将它们重定向到登录表单,这是一个由 ajax 驱动的灯箱。虽然最初的请求是在 html
格式,我需要改成js
.现在,我正在尝试这样:
def new
if user_signed_in?
@company = Company.new
else
redirect_to new_user_session_path, format: 'js'
end
end
不幸的是,它仍然将重定向视为在
html
中。格式。有没有办法让重定向被视为 js
?
最佳答案
我在 rails 3.2.13 n 中尝试过它的工作原理,我希望它可以解决
if user_signed_in?
.....
else
if request.xhr?
flash[:notice] = "Please login."
flash.keep(:notice)
render :js => "window.location = #{new_user_session_path}"
else
.....
end
end
关于ruby-on-rails - 将 redirect_to 格式更改为 :js in Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18522651/