2011-06-29 2 views
3

私はRuby on Rails 3.0.7を使用しており、私のアプリケーションにはUsers::Articleクラスがあります(名前空間になっています)。 routes.rbファイルでは、次のように述べている:ルートビヘイビアを変更するが、 "Convention over Configuration"を維持する

resources :users do 
    resources :articles 
end 

私はURL /articles/articles/new/articles/1/articles/1/editでそれにアクセスできるように、Users::Articleクラスに関連したルートを設定したいと思いますが、私はしたいと思いますRoR のコンフリクトを "システム"に引き続き使用してください。つまり、私は以下を使用したいと思います:

  • ;
  • views/users/articles/<file_name>.html.erbビューファイル。
  • 名前付きルート(<name>_pathおよび<name>_url)。
  • など "Railsのウェイラルビー" ...

私はそれをどのように行うことができますか?


いくつかの単語では、例えば、私は/articles/1パスが、それはusers/<user_id>/articles/1パスを検討していると、アプリケーションが正確に動作することを参照したいと思います。

答えて

0

これはエスケープな問題ではありませんが、ルーティングのパラメータに関連しています。

/articles/:idを使用し、コントローラ内の所有者(ユーザー)を参照することができます。

/users/:user_id/articles/:idは2つのパラメータをコントローラに渡します。 ActionDispatch::Routing::Mapper::Resources docsから

0

:shallow 

# Generates shallow routes for nested resource(s). When placed on a parent 
# resource, generates shallow routes for all nested resources. 

resources :posts, :shallow => true do 
    resources :comments 
end 

# Is the same as: 
resources :posts do 
    resources :comments, :except => [:show, :edit, :update, :destroy] 
end 
resources :comments, :only => [:show, :edit, :update, :destroy] 

# This allows URLs for resources that otherwise would be deeply nested such as a 
# comment on a blog post like /posts/a-long-permalink/comments/1234 to be 
# shortened to just /comments/1234. 
0

からこれを取得するには>すくいルート

users_articles  GET /articles(.:format)   {:action=>"index", :controller=>"users/articles"} 
        POST /articles(.:format)   {:action=>"create", :controller=>"users/articles"} 
new_users_article GET /articles/new(.:format)  {:action=>"new", :controller=>"users/articles"} 
edit_users_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"users/articles"} 
users_article  GET /articles/:id(.:format)  {:action=>"show", :controller=>"users/articles"} 
        PUT /articles/:id(.:format)  {:action=>"update", :controller=>"users/articles"} 
        DELETE /articles/:id(.:format)  {:action=>"destroy", :controller=>"users/articles"} 

我々はroutes.rbをして、これを必要とする

namespace :users, :path => '' do 
    resources :articles 
end 

注:私は使用しています:名前空間はあなたが述べたものです。

これが役に立ちます。

関連する問題