如何使用用户名为控制器添加前缀?

时间:2010-11-23 19:49:49

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

我在Rails 3中使用新路由时遇到一些问题。使用用户名为路径添加前缀的最佳方法是什么。现在我有以下代码:

resources :links, :path => '/:username' do
  put 'star', :on => :member
end

但它留下了空白。

编辑:

我想有一个网址:

/:username/links  
/:username/links/:id/star

我希望能够获取params [:username]

2 个答案:

答案 0 :(得分:3)

你可以使用scope,它更整洁:

 scope :path => ":username" do
   resources :links do
     put 'star', :on => :member
   end
 end

答案 1 :(得分:-1)

您应该使用嵌套资源

resources :users do
  resources :links do
    put 'star', :on=>:member
  end
end

然后,如果你在user.rb中实现to_param

def to_param
  username.parameterize
end

然后你会

/:username/links
/:username/links/23
/:username/links/23/star
相关问题