【问题标题】:Path params is not working when i send as json in rails当我在 Rails 中作为 json 发送时,路径参数不起作用
【发布时间】:2016-08-30 22:22:34
【问题描述】:

在路线上我有这样的东西

  match '/*parent/:term/' => "parent#match" ,

例如,当我在服务器中点击此 url 时,它是这样的

localhost:3000/women/jeans

当我点击 html 请求时的日志

Parameters: {"parent"=> "women", "term"=>"jeans"}

我点击 json 请求时的日志(localhost:3000/women/jeans.json)

Parameters: {"parent"=>{}, "term"=>"jeans"}

当我以 json 格式发送时,我不知道为什么父参数为空

【问题讨论】:

  • 远射,但请尝试将match '/*parent/:term/' 更改为match '/*parent/:term',不要使用斜杠。
  • 我怀疑这应该归咎于不同的路线,而不是你是否使用ajax。您也可以在浏览器地址栏中执行 .json 之一。在控制台上执行rake routes 并查看它是否为.format 案例生成了一条路由::resources 方法可以做到这一点,但我不知道match 方法是否可以做到。
  • @MaxWilliams 没有其他路线可以匹配这条路线。
  • Thq 问题更多是你设置的路由是否匹配,添加 .format 可能会破坏它

标签: ruby-on-rails ruby json rest routes


【解决方案1】:

您的match 之前是否有任何其他路线可能与您的网址匹配?

我刚刚尝试过,一切看起来都很好:

# routes.rb
match '/*parent/:term/' => 'parent#match', via: :get

# Generates:
  GET    /*parent/:term(.:format)                      parent#match

# http://localhost:3000/women/jeans
{"parent"=>"women", "term"=>"jeans"}

# http://localhost:3000/women/jeans.json
{"parent"=>"women", "term"=>"jeans", "format"=>"json"}

看起来问题出在其他地方。

【讨论】:

  • @Uzebekjon 值来自 wap 服务器的代理请求
  • 抱歉,没有得到什么值?无论如何,这个问题似乎与你原来的问题无关。
  • 路径参数值没有出现。有点奇怪的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
相关资源
最近更新 更多