简体   繁体   English

如何检查Ruby on Rails控制器中是否有查询字符串?

[英]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 我在Ruby on Rails应用程序上使用分页,并在请求中使用查询字符串参数定义页面

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 我想检查是否有查询字符串,如果没有,则显示正确的页面= 1,通过重定向到我认为的页面

Does anyone know how I can do this? 有谁知道我怎么做到这一点?

Thanks ahead for any help 在此先感谢任何帮助

Simply check, 只需检查,

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

Thats it ! 而已 ! Cheers!!!! 干杯!!!!

if params[:page].blank?
  redirect_to :page => 1
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何从控制器查询或过滤计算方法? - Ruby on Rails: How can I query or filter a computed method from the controller? 如何在Ruby on Rails中从控制器映射数据 - How can i map the data from controller in Ruby on Rails 如何从视图中修改控制器中的变量(红宝石在轨道上) - How can I modify a variable in the controller from the view (ruby on rails) Ruby on Rails 3-如何检查控制器中的radio_button_tag? - Ruby on Rails 3 - How Can I Check a radio_button_tag In My Controller? 应该从控制器查询数据库中的ruby on rails - ruby on rails should i query to db from controller 从Rails中的控制器中运行MySQL查询 - Run mysql query from a controller in ruby on rails 如何从 Ruby on Rails 的控制台调用控制器/视图帮助器方法? - How can I call controller/view helper methods from the console in Ruby on Rails? 如何在 Rails 上的 Ruby 中将两个参数从 controller 传递到 model? - How can I pass two params from controller to model in Ruby on Rails? 在Ruby On Rails中,如何删除与特定控制器相关的视图 - In Ruby On Rails, how can I remove views related to a specific controller 在Ruby on Rails中,如何将控制器中的所有动作映射到root? - In Ruby on Rails, how can I map all actions in a controller to root?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM