42

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

/page?param1=value&param2&value

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

/page

Thank you.

Martin
  • 11,216
  • 23
  • 83
  • 140

4 Answers4

79

Use path instead of fullpath.

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

Update:

As noticed by turboladen, "[ActionDispatch::Request][2] inherits from [Rack::Request‌][3]​, where request.path is defined".

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

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
rosenfeld
  • 1,730
  • 15
  • 19
28

what about a simple split:

request.fullpath.split("?")[0]
johnmcaliley
  • 11,015
  • 2
  • 42
  • 47
  • 5
    the source for #fullpath shows that it is a concatenation of #path and #query. Save some work by just using #path http://www.rubydoc.info/gems/rack/Rack/Request:fullpath (I understand this has been asked and answered, but it is still worth noting for future SO'ers) – Pat Newell May 27 '15 at 14:08
  • Just because an answer works, doesn't mean: it's the correct answer. The upvotes are an example of lacking conscientiousness. – Ekkstein Sep 17 '20 at 16:56
9

no need for splitting,

request.path_info

gives you just that

Mathieu J.
  • 1,932
  • 19
  • 29
3

regular expression

request.fullpath.gsub( /\?.*/, "" )
fl00r
  • 82,987
  • 33
  • 217
  • 237