我真的很难做出这样的路线
localhost:3000/api/search/:{this_can_be_anything}
我想点击搜索
Controller ,并将最终结果作为变量接收
如果您知道如何执行此操作,请告诉我
编辑:
我没有提到(或意识到很重要)的是 :{this_can_be_anything}
实际上会反射(reflect)一个网址,例如 david:office
所以,我想能够查找类似 localhost:3000/api/david:office 的内容并查看相关页面
最佳答案
您离实际语法已经不远了。像这样的东西可能会满足要求。
# config/routes.rb
get '/api/search/:query', to: 'api/search#index'
# app/controllers/api/search_controller.rb
module Api
class SearchController < ApplicationController
def index
query = params[:query]
# ...
end
end
end
上面应该将请求 GET/api/search/david:office
发送到 SearchController
的 index
操作。字符串“david:office”可通过 :query
参数获取。
关于ruby-on-rails - 如何在rails中制作可变路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248643/