Ben Walker
Ben Walker

Reputation: 2127

Rails link_to existing route

I'm trying to link to an action in my controller from a view. When I rake routes, I see

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

I think that's the route I want to hit. My controller is called document_versions_controller and it includes an action called download that I want to call. When I try

<%= link_to linkText, :document_version_file  %>

I get a routing error:

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

There is no files controller. I'm trying to hit the document_version controller. What am I doing wrong?

EDIT: How I am building my route:

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

EDIT 2: Can we step back and pretend I don't have a route in existence already? I have a view and a controller. The controller has def download, is named document_versions_controller.rb and has class DocumentVersionsController < ApplicationController. How can I make a route that hits that download method and call it with a link click in my view?

Upvotes: 0

Views: 134

Answers (3)

Ben Walker
Ben Walker

Reputation: 2127

My problem was that I was conflating two goals: building a route to hit my controller action, and building my api call url. The api url (which includes file and version_id) is being built in the controller action, and it is incorrect to try to build a route to match the api url. I modified my routes.rb so now it looks like this:

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

and my link_to in the view looks like this:

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

and my controller performs an api call with Faraday in the download action.

Upvotes: 1

Ganesh Kunwar
Ganesh Kunwar

Reputation: 2653

Simply use

<%= link_to linkText, :document_version_file_index_path(@document_version_file)  %>

Where @document_version_file is the document version of which you want to see all the files.

Edited: This will give the routes as your exceptions.

get 'document_versions/:document_version_id/files' => 'document_versions#index', as: : document_version_files

Upvotes: 0

tadman
tadman

Reputation: 211560

That route requires a :document_version_id argument, so you can't call it straight-up like that. The way to use that route would be:

document_version_file_index_path(@document_version_file)

I'm not sure why the route has index in the name, normally automatically generated routes don't.

The easy way to remember how those routes work is for every :x type argument in the path that's not optional, not inside brackets, you provide the value for that in the call in the order they appear.

A route to /:a/:b.(:c) can be called as x(a,b) or x(a,b,c), either way is valid, as :c is optional.

Upvotes: 3

Related Questions