Rails javascript window.location不工作
我正在尝试使用Javascript(Coffeescript)在Rails中路由到另一个页面 这是我的代码:Rails javascript window.location不工作,javascript,ruby-on-rails,coffeescript,Javascript,Ruby On Rails,Coffeescript,我正在尝试使用Javascript(Coffeescript)在Rails中路由到另一个页面 这是我的代码: # go to edit page serverused = window.location.host newurl = serverused + '/sites/' + node.id + '/edit' alert newurl window.location = newurl alert window.location return 警报新url=“
# go to edit page
serverused = window.location.host
newurl = serverused + '/sites/' + node.id + '/edit'
alert newurl
window.location = newurl
alert window.location
return
警报新url
=“localhost:3000/sites/2/edit”
警报窗口.location
(它甚至不应该到达)显示:“localhost:3000/sites/tree#node-5”-这是当前的url
谢谢你的帮助 您的
新URL
前面可能需要一个http://
或https://
。或者,您可以从当前位置获取协议:
newurl = window.location.protocol + '//' + serverused + '/sites/' + node.id + '/edit'
您还可以在Coffeescript中使用字符串插值(需要双引号):
你说得对:
window.location='google.com'
失败,而window.location='http://google.com
有效
newurl = "#{window.location.protocol}//#{serverused}/sites/#{node.id}/edit"