【问题标题】:Rails custom routes with params带有参数的 Rails 自定义路线
【发布时间】:2013-06-17 12:43:20
【问题描述】:

我正在尝试在我的网址中显示参数,所以我添加了

patient_record_path(:limit => 10)

我现在正在尝试正确路由它。 目前我收到错误

No route matches {:action=>"show", :controller=>"patient_record", :limit=>10}

我目前正在使用该路线

match 'patient_record/show&limit', :to => 'patient_record#show'

【问题讨论】:

    标签: ruby-on-rails parameters routing jruby


    【解决方案1】:

    您不应将限制添加到您的路线。只需像这样简单地定义您的路线:

    match 'patient_record/show', :to => 'patient_record#show', :as => 'patient_record_show'
    

    然而更好的解决方案是

    resources :patient_records
    

    这将创建以下路径助手:

    patient_records_path => "/patient_records" => 'patient_record#index'
    new_patient_record_path => "/patient_records/new" => 'patient_record#new'
    edit_patient_record_path(:id) => /patient_records/:id/edit => 'patient_record#edit'
    patient_record_path(:id) => "/patient_records/:id" => 'patient_record#show'
    

    更新:路径助手的错误使用

    我再次查看了您的问题,发现了另一个错误:show 的路径助手需要记录。正确的用法是:

    # path to show
    patient_record_path(@patient_record, :limit => 10)
    
    # path to index
    patient_records_path(:limit => 10)
    

    【讨论】:

    • 对不起,在我完成之前发布了答案。现在已经更新了:)
    • 我原来有资源 :patient_records 也没有用
    • 您有一个PatientRecordsController(复数形式,在 PatientRecords 中带有 s),操作为 show 对吗?
    • 是的,我所有的其他路线都相应地工作。正是当我在我的 create 方法末尾添加了 patient_record_path(:limit => 10) 时,这打破了
    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多