我正在尝试在Ruby中的控制器中检索我的网站的完整URL(包括https://)。我需要它以适合以下代码:
url = ""
uri = URI::parse(url)
id = uri.path.split('/')[2]
感谢。
答案 0 :(得分:1)
您应该使用request.original_url
http://api.rubyonrails.org/v5.1/classes/ActionDispatch/Request.html#method-i-original_url
答案 1 :(得分:1)
正如Fede所说,你可以在你的控制器request.original_url
中使用。此外,如果您在视图中有一些ajax,则可以将其作为数据传递给模型,然后传递给JS中带有window.location.href
的控制器。
答案 2 :(得分:1)
我终于通过一些帮助和调试找到了它。因为我在一个正在数据库中创建新对象的类中请求URL,所以我不得不使用.asoundrc file
pcm.!default{
type asym
playback.pcm{type plug slave.pcm "hw:0"}
capture.pcm{type plug slave.pcm "hw:1"}
}
ctl.!default{
type hw
card 0
}
:
INFO:root:Recording audio request.
File "/usr/lib/python3.5/runpy.py", line 193, in _run_module_as_main"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_codeexec(code, run_globals)
File "/home/pi/teamaker/grpc/pushtotalk_back.py", line 337, in <module>main()
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/click/core.py", line 722, in __call__return self.main(*args, **kwargs)
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/click/core.py", line 697, in main\ rv = self.invoke(ctx)
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/pi/teamaker/grpc/pushtotalk_back.py", line 326, in main
continue_conversation = assistant.converse()
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/tenacity/__init__.py", line 87, in wrapped_f
return r.call(f, *args, **kw)
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/tenacity/__init__.py", line 235, in call
do = self.iter(result=result, exc_info=exc_info)
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/tenacity/__init__.py", line 194, in iter
return fut.result()
File "/usr/lib/python3.5/concurrent/futures/_base.py", line 398, in result
return self.__get_result()
File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
raise self._exception
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/tenacity/__init__.py", line 238, in call
result = fn(*args, **kwargs)
File "/home/pi/teamaker/grpc/pushtotalk_back.py", line 145, in converse
self.conversation_stream.stop_playback()
File "/home/pi/teamaker/grpc/audio_helpers.py", line 288, in stop_playback
self._source.stop()
File "/home/pi/teamaker/grpc/audio_helpers.py", line 222, in stop
self._audio_stream.stop()
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/sounddevice.py", line 1235, in stop
_check(err, 'Error stopping stream')
File "/home/pi/teamaker/grpc/env_local/lib/python3.5/site-packages/sounddevice.py", line 2671, in _check
raise PortAudioError(msg)
sounddevice.PortAudioError: Error stopping stream: Unanticipated host API 0 error -77: 'File descriptor in bad state'
由于我的网址类似于“https://www.example.com/room/4”,因此 id 返回的值为4.