Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query parameters with url_for?

url_for([:edit, @post]) 

is working and generating /comments/123/edit. Now I need to add a query parameter, so that instead of

/comments/123/edit 

it is

/comments/123/edit?qp=asdf 

I tried url_for([:edit, @post], :qp => "asdf") but no go.

like image 244
randomguy Avatar asked Feb 17 '11 16:02

randomguy


People also ask

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

How do I get query params in Flask?

To access an individual known param passed in the query string, you can use request. args. get('param') . This is the "right" way to do it, as far as I know.

What does Flask's url_for () do?

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for , if there is a change in the root URL of your app then you have to change it in every page where the link is present.

CAN GET method have query parameters?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.


1 Answers

Use named routes.

edit_post_path(@post, :qp => "asdf") 
like image 143
Simone Carletti Avatar answered Oct 05 '22 22:10

Simone Carletti