【问题标题】:Rails route to action depending on parameterRails 根据参数执行操作
【发布时间】:2012-04-27 08:42:00
【问题描述】:

我正在使用 Rails 构建一个小搜索。 搜索应该像 /search?q="[query]"

有没有办法在没有“?q=”参数的情况下将请求路由到索引操作,否则路由到搜索操作? 我已经尝试过了,但它不起作用。

  controller :search do
    get ':index' => :index
    get ':index?:query' => :search
  end

【问题讨论】:

标签: html ruby-on-rails ruby


【解决方案1】:

在您的操作中添加

redirect_to root_path and return unless params[:q]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2018-05-30
    相关资源
    最近更新 更多