Symfony/Routing: Using POST to Avoid Query Params in URL - url

I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
Using Symfony, I have created a sign-up done page whose URL should be /signup/done
When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...
$this->redirect('#signup_done?id=' . $sign_up->getId());
The routing is as follows:
signup_done:
url: /signup/done
param: { module: SignUp, action: signupDone }
I have avoided the :id at the end because I don't want it in the URL.
But the resulting URL is /signup/done?id=1
Just as an experiment, I tried putting this on a template.
<?php echo link_to('Sign-up again', '#signup_done?id=1', 'post=true') ?>
Even when using post, the query parameter appears in the URL.
The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?

I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.
This kept the URL clean.

If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.
Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.
Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.

Related

Query Parameters in URL from a Rails form

I have a filter sidebar that starts with a Rails form_tag and contains a range slider and a bunch of check_box_tag.
It posts and filters fine. It even persists on the next page as I render it with the checkbox values.
However, if you refresh the page, or send a link to someone, the filters are lost.
The only way I've seen how to do it is to use redirect_to and merge the params, but I'd rather not make a second call.
How can I pass all the options as query params?
As you're not creating anything I would recommend using GET requests with query string parameters. So the query can be shared with the url.
Url for your example image would be something like:
http://www.yourwebsite.com/?max_price=5
Which gives you a params[:max_price] in controller.

Parsing params from url to next page in rails

I currently have a landing page /vouchers and then many pages linked to that for example /vouchers/1, /vouchers/2 etc.
I currently have the functionality that if this is entered /vouchers/1?offer=50 then the form on that page is sent with the offer included in the url.
Now I need to add the functionality so if /vouchers?offer=50 is entered, any page that is then navigated to, will keep these parameters. (for example /vouchers/1 should then include the offer from the url)
I see only one way.
store all "special" parameters in session
in any handler — if "special" parameters are in session — form new url with this parameters and redirect user there
Let me guess :
1. you have a model , named "Voucher"
2. In case you want to see the entry for , let's say , voucher#1 , you see URL voucher/1 (notice the singular form of the word 'voucher')
3. you assign the value of the offer by some interaction by user , for example - link or list . Let's say it's a link . Try this (HAML):
= link_to "Accept this offer and create a new voucher", new_voucher_path(:offer => #offer_value)
(in your case #offer_value = 50)
The following view (or partial ) will be able to read the value in :offer .
You can refer to this useful discusion .
I solved this, probably not in the best way. On the /voucher page, I added if statements to all the links, so if there's an offer in the url, the links are appended appropriately.

Rails - Reload action with new value in params

I have the following relation in my model:
A user can have several consumers
According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:
/:consumer_id/products/all
/:consumer_id/locals/all?params...
I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.
I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.
What's the best or appropriate way of doing so?
This is a link to the same page with the same parameters and one additional parameter:
<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>
If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.
Inside the view you can access both the current controller using controller_name and the current action using action_name
Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?
If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.
If you want to use that consumer id for links,
you may also use JQuery to update all links on your page
Or, you can submit the form to the server with new consumer_id.

Multi-step form using GET requests

We have the unusual requirement of a multi-step form through GET requests. So, instead of POSTing the forms, we're using GET requests to pass new parameters to the query. So, the basic idea is that there's a treatment, and a date passed to the query. A three-step form if you will.
Show available treatments, pick one
Show available dates (there's business logic in the background that figures these out)
Pick a time
The URL will go through the following states
site.com/bookings/new
site.com/bookings/new/[id|name_of_treatment] (by this, I mean it could either by the ID field or the name of the the treatment)
site.com/bookings/new/[id|name_of_treatment]/2010-12-12/
So, my route looks like this:
map.connect 'bookings/new/:massage_type/:date', :controller => :bookings, :action => :new
massage_type is synonymous with the treatment_id.
So my form_tag looks like this:
<% form_tag( {:action => "new"}, :method => "get" ) do %>
The problem I'm having is that I want it to just give me back the URL site.com/bookings/new/[id|name_of_treatment]/ but instead it gives back the following URL:
http://localhost:3000/bookings/new?massage_type[treatment_id]=1&commit=actionnew
I don't suppose anyone knows?
Forms that use GET are adding the input values as query parameters. There's no way to make the form post to a different URL, where the input values are part of the URL instead - it's just not supported by the HTML standard.
You could use URL rewrite to remap the incoming URLs of this type to the one you want, however that's not really a good solution, because this would result in a second request.
However, what I don't understand is why does the form need to do GET to that specific URL. Is it a requirement that these URLs can be constructed by the user manually, instead of using the form?
If there is no such requirement, I would advise to use standard POST form to http://localhost:3000/bookings/new and modify the form in the response based on the parameters in the POST body, as necessary.
Better yet, write some Ajax that would update the form according to the user's choice, without making a full form submit, until the user has finished all the choices.
By definition, the result of a GET request will have a query string (?param1=value1&param2=value2&...) in its URL. To get rid of those, you'll have to either start using POST or immediately redirect to the desired URL upon receiving a GET request.
I rather like the redirect approach because it doesn't show that confusing/annoying message about resubmitting POST data when the user refreshes their browser.

How to post a form with MVC controller?

How do I post a form to an external site using an ASP.NET MVC controller?
EDIT: Ok... I have a view with some data on it. After the user completes the form I need to do a postback with a specified form format to an external site (like for paypal). After I posted the constructed form I need to redirect the action to a new view
You have to do the POST on the server-side..
of which this guy has written a helper class to do Http Post in C# (pastebin-ed). Check it out.
Send the post with the PostSubmitter class and just render your view normally.
Basically, in situation like this one would create a HttpWebRequest, set Method to post and write the post data to the request stream. But the linked code already does that for you in a nice and cozy way.
So no need rewire anything.
You can just manually set the action in the form tag to wherever you want to post to...
edit -
That is to say that you should manually create the form tag..
Instead of:
<% using (Html.Form<Controller>("Action", c => c.Method())) { %>
You should use:
<form action="http://www.someotherwebsite.com/action">
How about sending a redirect to the browser.

Resources