开发者

How do I get only the query string in a Rails route?

开发者 https://www.devze.com 2023-01-23 16:40 出处:网络
I am using a route like this match \"/v1/:method\" => \"v1#index\" My intention here is capture the name of the api method and then send the request to that method inside the controller.

I am using a route like this

match "/v1/:method" => "v1#index"

My intention here is capture the name of the api method and then send the request to that method inside the controller.

def index
    self.send params[:method], params
end

I figured this wou开发者_StackOverflow中文版ld send the other parameters as an argument to the method, but it didn't work. So my question is how can I pass the non-method parameters in a query string?


#query_parameters does exactly what you want:

request.query_parameters

It's also the most efficient solution since it doesn't construct a new hash, like the other ones do.


Stolen from the work of a colleague. I find this a slightly more robust solution, since it will work even if there are changes to the path parameters:

params.except(*request.path_parameters.keys)


I sort of solved this problem by doing this:

params.except("method","action","controller")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号