Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if there is a query string from a Ruby on Rails controller?

I'm using pagination on our Ruby on Rails app and the pages are defined in the request with a query string parameter like

http://ourdomain.com/?page=2

I use this on our front page where there's no page number explicitly defined.

I'd like to do a check to see if there is a query string, and if not then display the correct page=1, by redirecting to that page I presume

Does anyone know how I can do this?

Thanks ahead for any help

like image 715
xiatica Avatar asked Jul 31 '11 10:07

xiatica


People also ask

Where is query string stored?

However, regardless of whether the query string is used or not, the whole URL including it is stored in the server log files. These facts allow query strings to be used to track users in a manner similar to that provided by HTTP cookies.

What does Before_action do in Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.


1 Answers

Simply check,

if request.query_string.present? 
  # Do your stuff here.
end

Thats it ! Cheers!!!!

like image 159
Manish Shrivastava Avatar answered Sep 24 '22 08:09

Manish Shrivastava