【问题标题】:Conflict routes and params name in Ruby on RailsRuby on Rails 中的冲突路由和参数名称
【发布时间】:2012-08-20 14:09:53
【问题描述】:

在我的网站中,用户可以通过example.com/category1、example.com/category2等方式访问分类,所以我将路由规则写成如下:

match '/:category' => 'home#category', :constraints => ShowCategory.new

ShowCategory 是一个确保用户访问的类别存在的类。同时,用户可以指定自己的个人域名,然后可以通过example.com/peter等url访问他/她的个人资料页面。所以还有一条路由规则:

match '/:user_domain' => 'Profiles#show'

因为我使用:constraints 作为类别路由,所以路由没有冲突。但是,在Profiles#show 操作中,我总是得到参数{'category' => 'peter'},而不是{'user_domain' => 'peter'}。

如何更正参数名称?我不希望配置文件控制器中有一个名为 category 的参数。

谢谢。

【问题讨论】:

  • 显示相关代码,您的ShowCategory 类,会有所帮助

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


【解决方案1】:

好吧,您命名路线的方式没有多大意义;它使路由引擎感到困惑。应该是categories/1 而不是category1。

至于配置文件,我想第一条规则和to_param 方法应该足够了。

# routes.rb
profile '/:user_domain', controller: 'profiles', action: 'show'
resources :profiles
resources :categories
# profile.rb
def to_param
    self.user_domain
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多