在 Rails 上收集路由的另一种方法的索引中出现 nomethoderror

nomethoderror in index for another method with collection routes on Rails

我想创建一个 excel 文件。当我尝试继续时 professionals/628/medical_records 显示以下消息

undefined method `report_medical_records_path' for #<#<Class:0x0000000d0eb4f8>:0x0000000d0ea080>

对于模板上的以下行,此行是 link 必须转到创建 excel 文件的路径。

.clearfix
  div.excel-export
    = link_to report_medical_records_path(format: 'xlsx'), id: 'report_medical_records_link', data: { original_href: report_medical_records_path(format: 'xlsx') } do
      i.fa.fa-file-excel-o style="margin-right:5px"
      span exportar historias

这是我的 indexreport 操作

def index
    @medical_records = MedicalRecord.for_organization(current_organization)

    medical_records_scope = @medical_records.joins(:owner,:target_user).preload(:owner,:target_user)
    medical_records_scope = medical_records_scope.like_with_target_user(params[:filter].strip) if params[:filter]
    @medical_records_list = smart_listing_create(:medical_records,
                                                 medical_records_scope,
                                                 sort_attributes: [[:name, "name"],[:profesional, "users.name"],[:target, "target_users_medical_records.name"]],
                                                 partial: 'medical_records/listing'
    )

    @medical_id = current_user.medical_id

    if params[:no_layout]=='true'
      render 'index', layout: false
    else
      render 'index'
    end
 end

def report
    medical_records_scope = begin_of_association_chain.order('active DESC')
    medical_records_scope = medical_records_scope.like(params[:filter].strip) if params[:filter]

    @medical_records_report = medical_records_scope

    respond_to do |format|
      format.xlsx {
        response.headers['Content-Disposition'] = 'attachment; filename="medical_records.xlsx"'
        render disposition: 'inline'
      }
    end
 end

以及路线:

resources :medical_records, controller: 'medical_records' do
      collection do
        get :report
      end
    end

更新状态

I'll keep before question upside, so remains outdated.

当前错误信息:

The action 'report' could not be found for MedicalRecordsController

现在我在路线中有这个:

resources :professionals do
    #...
    resources :medical_records, controller: 'medical_records', only: [] do
      collection do
        get :report
      end
    end
  end

MedicalRecordsController里面的action是:

def report
    medical_records_scope = begin_of_association_chain.order('active DESC')
    medical_records_scope = medical_records_scope.like(params[:filter].strip) if params[:filter]

    @medical_records_report = medical_records_scope

    respond_to do |format|
      format.xlsx {
        response.headers['Content-Disposition'] = 'attachment; filename="medical_records.xlsx"'
        render disposition: 'inline'
      }
    end
  end

为什么在控制器中找不到动作?

你要的route/action不标准rails。您需要在 routes.rb 中告诉它更多信息才能获得您想要的内容,特别是 操作 :

resources :medical_records, controller: 'medical_records' do
  collection do
    get :report, action: :report
  end
end

这将为您提供以下路线:

report_medical_records GET  /medical_records/report(.:format)   medical_records#report

已解决更新的问题!

动作在 private 内,然后向上移动。