【问题标题】:Error matching route with custom parameters in Rails 3 that worked in Rails 2.3在 Rails 3 中使用 Rails 2.3 中的自定义参数匹配路由时出错
【发布时间】:2012-01-27 17:24:11
【问题描述】:

我们正在将 Rails 2.3 应用程序转换为 3.0.11,但在迁移某些路由时遇到了问题。

在我看来,我们的自定义参数(:title 和 :name_recognition)似乎导致了一些问题。但是,我们使用的是命名路由,所以我非常不清楚 Rails 为什么要尝试通过控制器、操作和参数进行任何匹配。

我们得到的错误:

ActionView::Template::Error (No route matches {:controller=>"audience_tool", :action=>"show", :name_recognition=>"BandName", :title=>"", :id=>1388}):

这是来自 routes.rb 的有问题的路线:

match ':name_recognition/:title/:id' => 'audience_tool#show', :as => :show_page

这是“rake routes”的结果:

show_page  /:name_recognition/:title/:id  {controller=>"audience_tool", :action=>"show"}

这是我们用来构建 URL 的模型方法:

def url(options=Hash.new)
  options.reverse_merge!({
    :name_recognition => name_recognition,
    :title => title,
    :id => id
  })
  show_page_path options
end

这是我们如何调用方法:

link_to("The Link text", show.url)

堆栈跟踪,减去错误中显示的模板代码:

actionpack (3.0.11) lib/action_dispatch/routing/route_set.rb:425:in `raise_routing_error'
actionpack (3.0.11) lib/action_dispatch/routing/route_set.rb:398:in `generate'
actionpack (3.0.11) lib/action_dispatch/routing/route_set.rb:454:in `generate'
actionpack (3.0.11) lib/action_dispatch/routing/route_set.rb:482:in `url_for'
actionpack (3.0.11) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.11) lib/action_dispatch/routing/route_set.rb:195:in `show_page_path'
app/models/performance.rb:245:in `url'
app/views/performances/_show_banner.haml:12:in `block in _app_views_performances__show_banner_haml___933466686__622884308__332894942'
haml (3.1.4) lib/haml/helpers/action_view_mods.rb:93:in `block in capture_with_haml'
haml (3.1.4) lib/haml/helpers.rb:345:in `call'
haml (3.1.4) lib/haml/helpers.rb:345:in `block in capture_haml'
haml (3.1.4) lib/haml/helpers.rb:569:in `with_haml_buffer'
haml (3.1.4) lib/haml/helpers.rb:341:in `capture_haml'
haml (3.1.4) lib/haml/helpers/xss_mods.rb:61:in `capture_haml_with_haml_xss'
haml (3.1.4) lib/haml/helpers/action_view_mods.rb:93:in `capture_with_haml'
app/helpers/pretty_helper.rb:4:in `rounded_wrap'
app/views/performances/_show_banner.haml:4:in `_app_views_performances__show_banner_haml___933466686__622884308__332894942'

【问题讨论】:

  • title 好像是空的。
  • 命名路由是否声明在其等效资源声明之上?

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


【解决方案1】:

参数内部的空白标题属性是问题

动态参数路由声明为 -

 match ':a/:b/:c', :to => 'home#index', :as => :q

现在转到 Rails 控制台 -

ruby-1.9.3-head :005 > app.q_url(:a, :b, :c)
 => "http://www.example.com/a/b/c" 

ruby-1.9.3-head :006 > app.q_url(:a, :b, '')
ActionController::RoutingError: No route matches {:controller=>"home", :a=>:a, :b=>:b, :c=>""}

above route 期望 url 中的每个参数都存在。 如果您太确定某个参数可以为空,那么您可以将其定义为路由内的可选参数 -

match ':a/:b(/:c)', :to => 'home#index', :as => :q

ruby-1.9.3-head :010 > app.q_url(:a, :b, '')
 => "http://www.example.com/a/b?c=" 
ruby-1.9.3-head :011 > app.q_url(:a, :b)
 => "http://www.example.com/a/b"

【讨论】:

  • 哇,就是这样。谢谢。我们没有意识到空白参数与根本不传入参数是一样的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多