Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails how to request url except url params?

I am creating this link tag:

<link rel="canonical" href="<%= request.url %>" />

The problem is just it is the full url with params.

How do I request the url without any params?

like image 887
Rails beginner Avatar asked Mar 28 '12 17:03

Rails beginner


People also ask

What is the best way to get the current request URL in Rails?

You should use request. original_url to get the current URL.

How to access URL parameters in Rails?

Rails' params hash contains any request parameters (URL parameters or request payload) as well as routing parameters like :controller , :action , or :id . To access only URL parameters, use request. query_parameters . Routing params are available through request.


2 Answers

request.url.split('?').first

like image 160
James Avatar answered Sep 22 '22 23:09

James


request is an ActionDispatch::Request and that subclasses Rack::Request. Rack::Request has a path method that might interest you:

<%= request.path %>

If your request.url is http://example.com/where/is?pancakes=house%3F then request.path should be /where/is.

like image 40
mu is too short Avatar answered Sep 22 '22 23:09

mu is too short