开发者

Rails 3.1 routing error, Named parameter not working

I have a route setup for downloading a document which I need a named parameter for the document name so I get the doc name on the download document. I'm also passing a few unnamed parameters to identify the document.

Looks like this:

match "download/:name", :to => 'documents#download', :as => "doc_download"

And I have my link setup like this:

<%= link_to doc.pdf_name, doc_download_url(doc.pdf_name, 
    :prefix => doc.doc_prefix,:num => doc.doc_num, :change => doc.doc_change) %>

When I run the page I get the following error:

No route matc开发者_JAVA技巧hes {:controller=>"documents", :action=>"download", 
    :prefix=>"D", :num=>"002", :change=>0, :name=>"sdr_vor_000.pdf"}

The weird thing is the route shows up in my rake routes:

doc_download  /download/:name(.:format) {:controller=>"documents", :action=>"dow
    nload"}

Ideas?

Also if I remove the name parameter or make it optional, it works, but doesn't stick the name into the url. It just gets tacked onto the query string.


Your name parameter contains a period - by default, the part after the period is interpreted as the format, not part of the name parameter. To allow periods in the filename, amend your route like this (tweak the regex to your needs):

match "download/:name", :to => 'documents#download', 
  :constraints => { :name => /[a-z0-9\.]+/i }, :as => "doc_download"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜