【问题标题】:Why is my param being ignored in a Controller for a show view?为什么我的参数在控制器中被忽略以显示视图?
【发布时间】:2021-10-26 10:06:36
【问题描述】:

当我访问时:

网址/打捞/25605

ActiveRecord 显示错误 RecordNotFound

没有 ID 无法找到 Salvage

参数: {"id"=>"25605"}

当我在参数中包含“typeID”时,为什么 ActiveRecord 会搜索“id”?

控制器

class SalvageController < ApplicationController
  def show
    @salvage = Salvage.find(params[:typeID])
  end

型号

class Salvage < ApplicationRecord
  validates :typeID, presence: true
  validates :amount, presence: true
end

查看

<%= @salvage.typeID %>, <%= @salvage.amount %>

资源

resources :salvage

铁路路线

Prefix Verb   URI Pattern                                                                                       Controller#Action
                             salvage_new GET    /salvage/new(.:format)                                                                            salvage#new
                                    root GET    /                                                                                                 application#hello
                production_pages_copying GET    /production_pages/copying(.:format)                                                               production_pages#copying
                           salvage_index GET    /salvage(.:format)                                                                                salvage#index
                                         POST   /salvage(.:format)                                                                                salvage#create
                             new_salvage GET    /salvage/new(.:format)                                                                            salvage#new
                            edit_salvage GET    /salvage/:id/edit(.:format)                                                                       salvage#edit
                                 salvage GET    /salvage/:id(.:format)                                                                            salvage#show
                                         PATCH  /salvage/:id(.:format)                                                                            salvage#update
                                         PUT    /salvage/:id(.:format)                                                                            salvage#update
                                         DELETE /salvage/:id(.:format)                                                                            salvage#destroy

我第二次使用我自己的数据在 learnenough.com 上关注 ruby​​ on rails 第 6 版教程

【问题讨论】:

  • 您能否添加rake routes | grep salvage 的结果并从rails 服务器日志中请求参数
  • 啊哈! '打捞 GET /salvage/:id(.:format)' @DeepakMahakale 有趣...
  • 顺便说一下,在 rails 6.1 中,rake 路线被贬值,而有利于 rails 路线github.com/rails/rails/commit/…
  • @DeepakMahakale 我在原始问题中包含了 rails 路线的结果

标签: ruby-on-rails controller


【解决方案1】:

感谢 Deepak Mahakale,我运行了 rails routes 命令,发现:id(format) 被添加到了 GET 路由的末尾。

我在堆栈溢出中搜索了有关将 :id(format) 更改为其他内容的问题并遇到了:

Changing the id parameter in Rails routing

因此,我将路由器中的资源更改为:

resources :salvage, param: :typeID

显示视图现在可以正常工作了,尽管速度很慢 我会在资源这个话题上做更多的研究,以获得更清晰的理解。

我希望这个答案对任何可能偶然发现相同问题的人有用

【讨论】:

  • 只是一个提示,但type_id 是ruby 约定,使用snake_case
  • 感谢@Eyeslandic 的提示!我使用了我正在使用的 csv 中的名称 - 下次运行时我会记住这一点!
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多