Ruby on rails 处理在Rails 3中有一个嵌套资源
我有一个用户模型和一个关于模型。about模型是一个页面,用户可以在其中获得更多关于他们的信息,由于其性质,将其放在单独的模型上比放在用户模型中更合适 我希望能够将它路由到像/:username/about这样的位置,并获取该路径上的所有动词(get POST、PUT、DELETE) 这就是我已经拥有的Ruby on rails 处理在Rails 3中有一个嵌套资源,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个用户模型和一个关于模型。about模型是一个页面,用户可以在其中获得更多关于他们的信息,由于其性质,将其放在单独的模型上比放在用户模型中更合适 我希望能够将它路由到像/:username/about这样的位置,并获取该路径上的所有动词(get POST、PUT、DELETE) 这就是我已经拥有的 # routes.rb resources :users do resources :abouts end match ':username/about' => 'abouts#s
# routes.rb
resources :users do
resources :abouts
end
match ':username/about' => 'abouts#show', :as => :user_about
match ':username/about/add' => 'abouts#new', :as => :user_new_about
match ':username/about/edit' => 'abouts#edit', :as => :user_edit_about
在我的模型里
# about.rb
belongs_to :user
# user.rb
has_one :about
当我写一篇博文或发表一篇关于它的文章时,我把它理解为一场表演
Started POST "/roses/about" for 127.0.0.1 at Sun Feb 27 16:24:18 -0200 2011
Processing by AboutsController#show as HTML
我可能丢失了路由中的声明,但是当它与默认值不同时,为资源声明每个动词不是很混乱吗
最简单、更干净的归档方法是什么?您可以使用
范围和控制器块来减少赘述:
scope "/:username" do
controller :abouts do
get 'about' => :show
post 'about' => :create
get 'about/add' => :new
get 'about/edit' => :edit
end
end
产生:
about GET /:username/about(.:format) {:action=>"show", :controller=>"abouts"}
POST /:username/about(.:format) {:action=>"create", :controller=>"abouts"}
about_add GET /:username/about/add(.:format) {:controller=>"abouts", :action=>"new"}
about_edit GET /:username/about/edit(.:format) {:controller=>"abouts", :action=>"edit"}
当使用has_one
时,将其声明为路由中的单一资源可能是有意义的。意义
resources :users do
resource :about # notice "resource" and not "resources"
end
如果要覆盖新建/编辑的路径,请在资源/资源调用中添加一个:path\u names
选项:
resources:about,:path_names=>{:new=>'add',:edit=>'edit'}
还有很多关于路由的提示和技巧。这就是我一直在寻找的!这就是我一直在寻找的答案。这个答案更为荒谬。
resources :users do
resource :about # notice "resource" and not "resources"
end