Rails link_to现有路由

时间:2015-06-04 17:25:04

标签: ruby-on-rails ruby

我尝试从视图中链接到控制器中的操作。当我耙路线时,我看到了

document_version_file_index GET    /document_versions/:document_version_id/file(.:format)           file#index

我认为这是我想要的路线。我的控制器名为document_versions_controller,它包含一个我想调用的名为download的操作。当我尝试

<%= link_to linkText, :document_version_file  %>

我收到路由错误:

No route matches {:action=>"show", :controller=>"files"}

没有文件控制器。我试图点击document_version控制器。我做错了什么?

编辑: 我如何建立自己的路线:

resources :document_versions do
    resources :files
    member do 
      get 'download', :action => :download
      resources :document_version
    end
  end 

编辑2: 我们可以退后一步,假装我还没有路线吗?我有一个视图和一个控制器。控制器为def download,名为document_versions_controller.rbclass DocumentVersionsController < ApplicationController。如何制作符合下载方法的路线,并在我的视图中点击链接进行调用?

3 个答案:

答案 0 :(得分:3)

该路线需要:document_version_id参数,因此您无法像这样直接调用它。使用该路线的方法是:

document_version_file_index_path(@document_version_file)

我不确定为什么路线名称中有index,通常是自动生成的路线“

记住这些路由如何工作的简单方法是路径中的每个:x类型参数不是可选的,而不是在括号内,您按顺序为调用中的值提供值出现。

/:a/:b.(:c)的路由可以被称为x(a,b)x(a,b,c),两种方式都有效,因为:c是可选的。

答案 1 :(得分:1)

我的问题是,我正在混淆两个目标:建立一条路线来击中我的控制器动作,以及建立我的api呼叫网址。 api url(包括fileversion_id)正在控制器操作中构建,尝试构建匹配api url的路由是不正确的。我修改了我的routes.rb所以现在它看起来像这样:

 resources :document_versions do
    member do 
      get 'download', :action => :download
    end
  end 

并且视图中的link_to如下所示:

<%= link_to linkText, download_api_document_versions_path(:version_id => version.document_version[:id]) %>

我的控制器在下载操作中与法拉第进行api通话。

答案 2 :(得分:0)

只需使用

[
         {
            "NOMBRE": "TEST",
            "ESPECIALIDAD": "TEST",
            "ESPECIALIDAD2": "TEST PEDIATRICA",
            "ESPECIALIDAD3": "",
            "CEL": "2222009374947478474777",
           etc.....
        }, {
            "NOMBRE": "TEST",
            etc.....
        }
]

其中<%= link_to linkText, :document_version_file_index_path(@document_version_file) %> 是您要查看所有文件的文档版本。

编辑: 这会将路线作为例外。

@document_version_file
相关问题