Railsã§ã®URLè¨è¨ãèãã¦ã¿ã(4) ã¹ã©ãã·ã¥ã¨ãæã£ã¦ãããé¢ä¿
ä»åã¯ãURLã®ã¹ã©ãã·ã¥ã¯ã©ã®ãããªã¨ãã«ä½¿ãã°ããã®ãèãã¦ã¿ã¾ããã
resourcesã®ãã¹ã
URLã¯ã¹ã©ãã·ã¥ã/ãã§é層æ§é ã表ãã¾ããRailsã§ã¯ãroutes.rbã§resourcesããã®ããã«ãã¹ããã¦ä½¿ãã¨ã
resources :users do resources :articles end
ãã®ãããªURLãã§ãã¾ãã
http://example.com/users http://example.com/users/1 http://example.com/users/1/articles
/users
ã§ãã¦ã¼ã¶ã¼å
¨ä½ãã /users/1
ã§ãã¦ã¼ã¶ã¼ã®1çªããæããããã« /users/1/articles
ã§ãã¦ã¼ã¶ã¼ã®1çªãã®æ¸ãããè¨äº(article)å
¨ä½ããæãããã§ãã((ããã« /users/1/articles/2
ã§ãã¦ã¼ã¶ã¼ã®1çªãã®æ¸ãããè¨äºã®2çªããæãURLãã§ãã¾ãããRailsã§ã¯idã¯åºæ¬çã«ã°ãã¼ãã«ã§ãããã¨ããã /articles/2
ã§ååãªã®ã§ãã¾ãå¿
è¦ã¨ã¯æãã¾ãããresources :articles, :only => :index
ãªã©ã¨ãããã¨ã§é¤ããã¨ãã§ãã¾ãã))
ãã®ããã«ããæã£ã¦ãããhas_one(1:1)ãhas_many(1:n)ã®é¢ä¿ãããã¨ãã«ãé層ã¨ã¿ãªãã¦ã¹ã©ãã·ã¥ã§ã¤ãªããURLãã¤ããã®ãä¸è¬çã«ãªã£ã¦ãã¾ãã
ä¾ãã°ãuserã¨articleãããããhas_many commentsã®é¢ä¿ã®ã¨ãã
/users/1/comments /articles/2/comments
ã¨ããURLãã¤ãããã¨ãã§ãã¾ããæå³ãåãããããã§ããã
belongs_toã®ã¨ãã¯ï¼
ã¨ããã§ãåãä»ä½ã£ã¦ããRailsã¢ããªã§ã¯ããã¦ã¼ã¶ã¼ã®1çªãããã³ã¡ã³ãããããè¨äºå
¨ä½ãã®URLãå¿
è¦ã«ãªãã¾ãããcomment belongs_to articles ã®é¢ä¿ã§ãã
ãã¦ãããã¯ã©ã表ç¾ããã°ããã§ãããï¼
(1)
/users/1/comments/articles
ããä¸ããã®é¡æ¨ã§ãããã¼ããbelongs_toã®ã¨ãã¯ãªããéåæãããã®ã¯åã ãã§ãããããã³ã¡ã³ããè¨äºãæã£ã¦ããããã§ã¯ãªãã®ã§ãã
(2)
/articles?commented_by_user_id=1
è¨äºå ¨ä½ãããæ¡ä»¶ã®ãã©ã¡ã¼ã¿ã§æ½åºããã¨ããæããç´ ç´ã§æå³ã¯ããããããã®ã§ãããããã¡ãã£ã¨ããããªæãã«ã§ããªãã§ããããã
(3)
/users/1/commented/articles
(1)ã¨ã»ã¨ãã©å¤ãããªããããï¼ã¨æãããããããã¾ãããã大ããéãç¹ã¯ãcommentedãã§ããããã¯è¨ããªãã°ãcommented_resourcesãã®ç¥ã§ãè¨äºããæã£ã¦ãããé¢ä¿ã«ãªã£ã¦ãããã¨ããããã¾ãã
ã³ã¡ã³ããã¤ããããã®ãè¨äºã ããªãã°ãã¹ã©ãã·ã¥ã¯ä½¿ããã« /users/1/commented_articles
ã¨ãã¦ãããã®ã§ãããä»ä½ã£ã¦ããã¢ããªã§ã¯è¨äºä»¥å¤ã®ãã®ã«ãã³ã¡ã³ããã¤ããããããã«ãªã£ã¦ããã¨ãããã¨ãããã¦ãªã½ã¼ã¹åããarticlesãã«çµ±ä¸ãããã¨ã§ãåãã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ãããã¨ã示åããå¹æãããã¾ãã
routes.rbã®æ¸ãæ¹
(3)ã§ã¯ããæ¸ãã¾ãã
resources :users do resource :commented, :only => :show do resources :articles, :only => :index end # CommentedsControllerãä¸è¦ãªå ´åã¯ä»¥ä¸ã§ããã # resources :articles, :path => 'commented/articles', :as => 'commented_articles', :only => :index end
ã«ã¼ãã£ã³ã°ã¯ãããªãã¾ããï¼usersé¨åã¯çç¥ï¼
user_commented_articles GET /users/:user_id/commented/articles(.:format) {:action=>"index", :controller=>"articles"} user_commented GET /users/:user_id/commented(.:format) {:action=>"show", :controller=>"commenteds"}
æ¬æ¥ãcommentedãã¯åæ°ãªã½ã¼ã¹ã§ã¯ãªãã®ã§ãããresourcesã®ãã¹ãã§ã¯å¿
ãidãå
¥ã£ã¦ãã¾ãï¼collection do ... end ã« resources ããã¹ããããã¨ãªãããããããªãã®ã§ãåæ°æ±ãã«ãã¦resourceã使ç¨ãã¾ãã/users/:user_id/commented/:commented_id/articles
ã¨ãªãï¼
/users/:user_id/commented
ã¨ããURLã¯ä½¿ããªãå ´åãå¤ãã®ã§ã:pathã¨:asã使ã£ãæ¸ãæ¹ãOKã§ããä»ã¯ä½¿ããªãã¨ãã¦ãcommentedããªã½ã¼ã¹ã¨ãã¦ãæå³ãæã¤ãã¨ãããã¨ãéè¦ã§ããã¡ãªã¿ã«ãscopeã使ã£ã¦
scope :path => 'commented', :as => 'commented' do resources :articles, :only => :index end
ã§ãã§ããã¯ãã§ããããªããæå
ã§ã¯ãã¾ãããã¾ããã§ããã
ã¹ã©ãã·ã¥ã®æ¡ä»¶
ã¹ã©ãã·ã¥ã使ã£ã¦ãA/B/Cãã¨ããURLãã¤ããã¨ãããããããã¨ãè¨ããã¨æãã¾ãã
- Aã¯BããBã¯Cãæã£ã¦ããé¢ä¿
- ãA/BãããAãããªã½ã¼ã¹ã¨ãã¦æå³ãæã¤
ããã¯ãé層æ§é ã¨èããã¨èªç¶ãªãã¨ã§ãã
ãã¡ãããé層æ§é ããªãã¾ãªããã¼ã¿ãããã®ã§ãããã表ç¾ããã¨ãã¯å¥ã®æ段ãèãããã¨ã«ãªãã¾ããããã¯ã¾ããã¿ãã§ããã°æ¸ãã¦ã¿ããããï¼