Justin D.
Justin D.

Reputation: 4976

Check if a url parameter exists and render approprietly in 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 ?.

Upvotes: 2

Views: 1774

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270609

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.

Upvotes: 2

Norto23
Norto23

Reputation: 2269

have you tried params[:ui-state] == dialog or params[:ui-state].blank? / params[:ui-state] == ""

Upvotes: 1

Related Questions