我正试图通过路径将某些内容传递给params
哈希。我的代码是:
的routes.rb
get :search, controller: :restaurants
restaurants_controller.rb
before_action :force_json, only: :search
def search
@restaurants = Restaurant.where("name LIKE ?", "%#{params[:q]}%").limit(5)
end
private
def force_json
request.format = :json
end
由于before
操作,我们不应该将路径称为search.json
;仅search
就足够了。但是,当我尝试将参数:q
传递给params
哈希时,如下所示:
localhost:3000/search&q=A
我最终得到了这个错误:
No route matches [GET] "/search&q=A"
答案 0 :(得分:1)
答案 1 :(得分:1)
好吧,如果您在查询字符串中表示您应该将其作为
传递localhost:3000/search?q=A
否则,您可以将参数放在路线定义中
get 'search/:q'