Rails 3“无路由匹配”错误与自定义命名路由

时间:2012-02-04 01:16:07

标签: ruby-on-rails ruby ruby-on-rails-3

我在routes.rb

中有这个自定义路线
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses

我构建了一个这样的链接:

<%= link_to business.name, business_permalink_path %>

但是,每当我访问带有该链接的页面时,都会收到此错误:

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

我尝试反转路线顺序:

resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink

这不起作用。如果我将链接更改为:

,它会起作用

show action存在并在文件controllers / business_controller.rb中定义。我想使用永久链接创建自定义URL。

我是Rails的新手,我知道我只是遗漏了一些东西。我错过了什么?

2 个答案:

答案 0 :(得分:5)

试试这个:

<%= link_to business.name, business_permalink_path(business.permalink) %>

答案 1 :(得分:-1)

试试这个:

match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink

更多信息:http://railscasts.com/episodes/203-routing-in-rails-3