Ruby-on-rails request.fullpath 不带参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5265943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:38:19  来源:igfitidea点击:

request.fullpath with no parameters

ruby-on-railsrubyruby-on-rails-3

提问by Martin

request.fullpath gives the fullpath plus any parameter passed, such as

request.fullpath 给出完整路径加上任何传递的参数,例如

/page?param1=value&param2&value

How can I get just the page with no params? Like

我怎样才能得到没有参数的页面?喜欢

/page

Thank you.

谢谢你。

回答by rosenfeld

Use pathinstead of fullpath.

使用path代替fullpath

Although not documented, request.pathworked for me. I usually use my plugin rails_web_console for playing with the request object.

虽然没有记录,但request.path对我有用。我通常使用我的插件 rails_web_console 来处理请求对象。

Update:

更新:

As noticed by turboladen, "[ActionDispatch::Request][2]inherits from [Rack::Request?][3]?, where request.pathis defined".

正如turboladen所注意到的,“[ActionDispatch::Request][2]继承自[Rack::Request?][3]?request.path定义的地方”。

pathis not documented there, but the source displays script_name + path_info.

path那里没有记录,但源显示了 script_name + path_info。

回答by johnmcaliley

what about a simple split:

一个简单的拆分怎么样:

request.fullpath.split("?")[0]

回答by Mathieu J.

no need for splitting,

无需拆分,

request.path_info

request.path_info

gives you just that

给你这个

回答by fl00r

regular expression

正则表达式

request.fullpath.gsub( /\?.*/, "" )