简体   繁体   English

Rails route.rb帮助

[英]Rails routes.rb assistance

thanks in advance for your help!! 在此先感谢您的帮助!!

I have in routes.rb : 我在routes.rb

get 'api/streets:name' => 'streets#get_by_name', as: "get_by_name"

I have in streets_controller.rb : 我在streets_controller.rb

ids = params[:name]

I have in Javascript: 我用Java语言编写:

const params = encodeURI('name[]=1&name[]=2')
fetch(`/api/streets?${params}`)

When I call the api from the front end, I get the following log message: 当我从前端调用api时,会收到以下日志消息:

Started GET "/api/streets?name%5B%5D=2&name%5B%5D=5" for 127.0.0.1 at 2019-09-06 17:10:59 -0700
   Rendering pages/index.html.erb within layouts/application
Processing by PagesController#index as */*
   Parameters: {"name"=>["2", "5"], "path"=>"api/streets"}
   Rendered pages/index.html.erb within layouts/application (7.1ms)
   Rendering pages/index.html.erb within layouts/application

Why is it using the PagesController and not the StreetsController? 为什么使用PagesController而不是StreetsController?

In all other cases where I'm getting and posting and putting and deleting on the api, the router knows what controller to use. 在我在api上进行获取,发布,放置和删除的所有其他情况下,路由器都知道要使用哪个控制器。 It's just this one case when I'm using array parameters where it's routing to the wrong controller. 当我使用数组参数路由到错误的控制器时,就是这种情况。

It's probably just something dumb I've done. 这可能只是我做过的蠢事。

Your route expects a path like: /api/streets123 - where params[:name] would be equal to "123" and no, that's not a typo. 您的路线预期路径为: /api/streets123其中params[:name]等于"123" ,不,这不是错字。

You should just use: 您应该只使用:

get 'api/streets' => 'streets#get_by_name', as: "get_by_name"

If you need to enforce the existence of the :name parameter then you should use the :constraints option . 如果需要强制存在:name参数,则应使用:constraints选项

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM