How to get the previous URL in Rails? Not the URL that you got a request from, but the one before that?
Because I'm getting an AJAX request, and I need the URL for the page they are currently on (or the URL before the AJAX).
Try to use HTTP_REFERER.
In Rails: request.referrer or request.headers["HTTP_REFERER"]
Use
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
here is more details.
In a web application there is no such thing as a previous url. The http protocol is stateless, so each request is independent of each other.
You could have the Javascript code, that sends a request back, send the current url with the request.
Just came across this question and it related to something simple I was doing presently.
A simple solution I have employed before when using ajax wizard style apps is to store two session variables which contain the previous and current request (with all params).
I just add these two lines to my application controller
session[:previous_request_url] = session[:current_request_url]
session[:current_request_url] = request.url
http://railscasts.com/episodes/246-ajax-history-state -> great for ajax
http://ethilien.net/archives/better-redirects-in-rails/ -> You could put in session as many previous url as you want.
Related
I'm adding disqus commenting to some articles on our site and all URLs are SEO friendly.
This means that, if the title of the article changes so will the URL of that article, which will discard the previous disqus comments (linked to the previous version of the URL).
The solution would be to strip out the title of the article from the URL before passing it to Disqus.
So I need to turn "http://mydomain.com/article/123-myarticle/section/1-sectiontitle" into "http://mydomain.com/article/123/section/1"
What is the easiest way to do this?
Thanks!
PS: I'm very new to Rails (i'm taking over a developed project)
You don't need to extract anything from the URL.
All you need to give to Disqus is a unique id.
So you can add a method to your model, called disqus_id for instance:
def disqus_id
"name_of_your_model_#{id}"
end
and then, in the javascript:
disqus_identifier = "<%= #your_model.disqus_id %>";
I have a URL in this format http://0.0.0.0:3000/#&ui-state=dialog. I want to check for the existence of ui-state so I can render approprietly a page. I tried this
params.has_key?(:'ui-state')
But it did not work.
How can I check for the existence of ui-state?
Solution
I am using jQuery mobile. What seems like a url parameter is actually an internal attribute of jQuery mobile. That's the reason why there are no ?.
If the partial example URL you posted (http://.../&ui-state=dialog) is your actual URL, it is incorrect. Your query string must begin with a ? as in http://.../?ui-state=dialog. An & cannot start the querystring.
Update
Since this is from jQuery and not a real URL parameter, it won't appear in params in Rails. Instead you'll need to parse it out of the request.url:
ui_state = /&ui-state=([a-z]+)$/.match(request.url)
puts ui_state[1]
Update2
Since the &ui-state follows the url hash #, it will not be available in the request.url. The hash is strictly a client-side component used by the browser, and isn't sent to the server in the HTTP request.
have you tried params[:ui-state] == dialog or params[:ui-state].blank? / params[:ui-state] == ""
In my controller action I return a view and I also need to update the url in the browser
so if the request url is test.site.testsite.com I want to change it to search.site.testsite.com how can I do this.
Thanks
So I'm not positive about sub-domains but HttpContext.RewritePath() allows you to modify the URL. This should give you a start.
You can achieve the above by following,
In IIS host two WebSites (test.site.testsite.com and search.site.testsite.com) both pointing to same physical/virtual directory.
In the Controller action , you can use a filter attribute which will redirect all search request to search.site.com.
I've got search form, which contains checkboxlist which is binded to my model. So when I set GET method to form I got long url:
(I even have exception:
The length of the query string for this request exceeds the configured maxQueryStringLength value.)
it's expecting, [0].IsSelected=false&[0].Id=6&[1].IsSelected=false...
But I would like url like this
www.domain.com/Action/Comma-separated-selected-idx
for example:
www.domain.com/Search/1,6,7
How can I fix,edit form get action? Thanks
I would do a POST instead, then redirect to the URL you desire.
OR
You could capture the form with some JavaScript and build the URL there.
agreed, POST or JS are your best options, I'd opt for a POST.
I'm trying to perform a redirect from one controller to another while passing along some parameters. Unfortunately the value of the parameters are long and obnoxious looking in the GET query string. Is there anyway I can use POST instead on the redirect to get a nicer looking URL?
Thanks.
As everyone said, you cannot redirect to post.
However, you can avoid ugly URLs by sticking your values in TempData instead of the route.
You can't do a proper POST redirect, but you can use JavaScript in the browser to mimic a POST redirect:
<form id="myform" action="http://mywebsite.com/">
<input name="myparameter" value="12345" type="hidden" />
</form>
<script>
document.getElementById("myform").submit()
</script>
It's not a true redirect and it won't perform as well as a redirect, but this should work.
A "Redirect" is a GET. If you're doing a real direct what you're doing is essentially informing the browser to go to another url. The browser than makes another http call (using GET) to the new url. The url may contain parameters but it will always be a GET and not a POST.
What you could do is store some data in session and then when the second (redirected) request comes in, you can access these values from session.
Sorry, no. Redirects via POST are simply not supported in the HTTP spec. Most clients implementing the common kinds of redirects (301 - Permanent and 302 - Temporary) issue a GET request to the new location.