Check if a url parameter exists and render approprietly in Rails - ruby-on-rails

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] == ""

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.

accessing previous URL in rails [duplicate]

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.

ZF2: Who should escape & delimiter for href when using Url helper?

When I use url helper to generate url for route with query params and then add that url to link href, validator warns me, that there is unescaped & in attribute:
▲
I tried to search but still I'm not sure who is responsible for escaping that.
Router generates url but that might be used anywhere, not only in html attribute, so it correctly does no escaping in his case.
Url helper does not change anything in that url but it is meand for use in html so it might done here
View template - there url is put inside href attribute, so it might be here too
I couldn't find any clue how to decide this and if fill an issue with zf2 about this.
EDIT: html/php code from paginator
<<
generates html
<<
and from what I found it should be
<<
I would argue that the current behavior (not HTML entity encoding) is correct and it is up to the developer to encode HTML entities, when appropriate.
For instance you may want to use the view helper inside a <script> tag, where the HTML entities would be uncalled for.

Asp.Net MVC Form[GET]. Correct action url

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.

AJAX request with Rails 3 and jQuery is being processed as HTML.

I have a form that i want submitting with AJAX, using rails 3 and jquery. The problem that I am facing is that rails is responding to the AJAX request as HTML too. After a little search I found that this has something to do with the correct Accept headers not being passed. How do i fix this?
This is the controller code
respond_to do |format|
format.js { render 'user/create' }
format.html { redirect_to ((params[:feed][:url].nil?)?url_for(:home) : params[:feed][:url]) }
end
It seems to work on a friends firefox, and on my chrome too, smthng wrong with my firefox?
UPDATE: It seems that the error arises only when I use a proxy service as JonDo, which probably changes the accept headers... Is there a way to force rails to use js format if the X-requested-by header is present?
Thanks
Your controller code looks correct. Are you sure that you have added the .js suffix to the AJAX URL in your form? That's how the responder knows what format you want for the response. The default format is HTML so omitting the suffix would look like it's responding to a HTML request.
You likely didn't set the dataType properly. See the docs (this is &.post) If you set dataType to "js" you'll be all good!

Resources