【问题标题】:Rails ignores collection route and routes to show action insteadRails 会忽略收集路线和路线以显示操作
【发布时间】:2016-02-16 05:02:54
【问题描述】:

我有以下路线:

resources :transactions do
  collection do
    post :detail
  end
end

当我到达http://localhost:3000/transactions/detail 时,rails 给了我这个错误:

ActiveRecord::RecordNotFound in TransactionsController#show

找不到 id=detail 的交易

这让我觉得表演路线优先于我的收藏路线,但我不知道为什么。与此问题非常相似:Rails ignores collection route and goes with show action instead,但我没有重复的资源 :transactions 条目。

【问题讨论】:

  • @RahulSingh 在这里是正确的 - 如果您正在执行显示操作,那么您正在发送 GET 请求 - 而不是 POST。您不能只在地址栏中输入http://localhos:3000/transactions/detail,使用cURL 或Postman 或更好的request spec 来测试它。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

拉胡尔是对的。当您在浏览器中访问网址时,您正在发出 GET 请求。试试这个:

resources :transactions do
  collection do
    get :detail
  end
end

如果这是您嵌套的唯一路线,则可以将其缩短为:

resources :transactions do
  get :detail, on: :collection
end

【讨论】:

  • 谢谢 Brent,我认为我的 link_to 一定有问题:,解析为 localhost:3000/transactions/detail。我认为你已经为我指出了正确的方向。谢谢。
  • 如果您需要解决方案方面的帮助,我很乐意与您交流
猜你喜欢
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-17
  • 2018-06-26
  • 1970-01-01
相关资源
最近更新 更多