ã¬ãŒã«å
ã®ã«ãŒãã¯éåžžã«éèŠã§ãã ããããåœé¢ã¯ããããã«æ³šæãæãããšããã§ããŸããã ç¹ã«ãscaffoldã³ãã³ãã䜿çšããå Žåã¯ããã¹ãŠèªåçã«åŠæ¹ãããŸãã ããããããæç¹ã§ãéæšæºã®ã«ãŒããäœæããå¿
èŠããããŸãã ããã¯ããããžã§ã¯ãã®configãã©ã«ããŒã«ããroutes.rbãã¡ã€ã«ã«ã¢ã¯ã»ã¹ãããšããæ¥ãããšãæå³ããŸãã
ã«ãŒããšã¯
ã«ãŒãã¯ããµã€ãäžã®ã«ãŒãïŒãã¹ãURLïŒã®ã·ã¹ãã ã§ãã ã«ãŒãã®ãããã§ããŠãŒã¶ãŒã«ãšã£ãŠçŸããæ確ãªãªã³ã¯ãäœæã§ããŸãã
mysite.ru/articles/2008/november/13ã®ãããªãªã³ã¯ãå
¥åãããšã2008幎11æ13æ¥ã®ãã¹ãŠã®èšäºã衚瀺ããããªã³ã¯
mysite.ru/shop/shoesã䜿çšãããšãåºèããéŽã«ã¿ãã°ã衚瀺ãããŸãã ããã«ããããµã€ãã®ãã£ã¬ã¯ããªæ§é ã¯å€ãããŸããã ãã¡ã€ã«èªäœã®å Žæã«è§Šããããšãªãããã€ã§ãã«ãŒããå€æŽã§ããŸãã ããããããããã¹ãŠæ©èœããããã«ã¯ãã«ãŒããæ§æããå¿
èŠããããŸãã
ç·Žç¿ãã
ã·ã£ãŒãã³ã«ãªããã¹ããããžã§ã¯ããäœæããŸãããã ïŒãããåããŠã®å Žåã¯ãã³ã¡ã³ãã§ãã¬ãŒã«ã®ã€ã³ã¹ããŒã«ãšã¢ããªã±ãŒã·ã§ã³ã®äœæã®ããã»ã¹ã«ã€ããŠèª¬æã§ããŸãïŒã
rails routes
cd routes
ããã£ã ãããžã§ã¯ããäœæãããäœæ¥ãã©ã«ããŒã«å
¥ããŸããã ããã«ã«ãŒããæ¥è¥²ããŸãïŒ
rake routes
ãã®ã³ãã³ãã¯ã2è¡ã®æšæºã«ãŒããæäŸããŸãã
/:controller/:action/:id
/:controller/:action/:id.:format
ããã¯ããããã®2ã€ã®ã«ãŒã«ã«ãã£ãŠURLã解æãããããšãæå³ããŸãã
ïŒã³ã³ãããŒã©ãŒã¯ã³ã³ãããŒã©ãŒ=ïŒã§ãã ããã¯
MVCã³ã³ããŒãã³ãã§ãããã»ãšãã©ã®å Žåããã¥ãŒïŒHTMLïŒãšã¢ãã«ïŒããŒã¿ããŒã¹ãªã©ïŒã®éã®ä»²ä»ãšããŠæ©èœããŸãã ããã«æ確ã«ãªããŸãããã»ãšãã©ã®å Žåããã§ã«ãããäœã§ããããç¥ã£ãŠããŸãã
ïŒã¢ã¯ã·ã§ã³ã¯ãã³ã³ãããŒã©ãŒã®åŒã³åºãããã¡ãœããã§ãã éåžžãã³ã³ãããŒã©ãŒã«ã¯å€ãã®ã¡ãœããããããŸãã
ïŒid-idã®äœæã®çŠæ¢ãæ瀺çã«æå®ããªãå Žåãããã©ã«ãã§ãã¹ãŠã®ã¢ãã«ïŒããŒã¿ããŒã¹ããŒãã«ïŒãidãã£ãŒã«ãã§äœæãããŸãã ãããã£ãŠãã¢ãã«ã®èŠçŽ ã«ã¯idããããŸãã ãŸããç¹å®ã®ã¢ãã«èŠçŽ ãåé€/ç·šé/äœãããå Žåã¯ããã®IDãã³ã³ãããŒã©ãŒã«æž¡ãå¿
èŠããããŸãã
ããã£ã ãã¥ãŒã¹éèªãäœæããŸãããã ãã®ããã«å¿
èŠãªãã®ïŒ
-ããŒã¿ããŒã¹å
ã®ãã¥ãŒã¹ããŒãã«ïŒã¢ãã«ïŒã ããŒã¿ããŒã¹ã«ã¯ãèšäºã®ã¿ã€ãã«ïŒã¿ã€ãã«ïŒãèšäºã®èè
ïŒèè
ïŒãèšäºèªäœïŒèšäºïŒãä¿åããŸãã
-ããŒã¿ããŒã¹ãæäœããããã®äžé£ã®ã¡ãœããïŒã³ã³ãããŒã©ãŒïŒ
-ãã¥ãŒã¹ã®å
¥åãç·šéãé²èŠ§ã®ããã®HTMLãã©ãŒã ïŒæåºïŒ
ãããã¯ãã¹ãŠåå¥ã«äœæã§ããŸãã ããããä»åºŠã¯ã¿ã¹ã¯ãåçŽåããscaffoldé¢æ°ã䜿çšããŠæ¢è£œãã¡ã€ã«ã®ããã¯ãçæããŸãã
./script/generate scaffold Magazine title:string author:string article:text
äžèšã®ãã¹ãŠãäœæããŸããïŒãã«ããŒãäœæããŸããããããã«ã€ããŠã¯ãã€ãïŒã ãŸããè¶³å ŽããŒã èªèº«ãå¿
èŠãªã«ãŒããäœæããŸããã Rake routesã³ãã³ããå床å®è¡ãããšãå€æ°ã®æ°ããã«ãŒããæãèœã¡ãŸã
magazines GET / magazines {ïŒã³ã³ãããŒã©ãŒ=> "éèª"ãïŒã¢ã¯ã·ã§ã³=> "ã€ã³ããã¯ã¹"}
format_magazines GET /magazines.:format {ïŒcontroller => "magazines" ,: action => "index"}
POST / magazines {ïŒã³ã³ãããŒã©ãŒ=> "éèª"ãïŒã¢ã¯ã·ã§ã³=> "äœæ"}
POST /magazines.:format {ïŒcontroller => "magazines" ,: action => "create"}
new_magazine GET / magazines / new {ïŒã³ã³ãããŒã©ãŒ=> "éèª"ãïŒã¢ã¯ã·ã§ã³=> "æ°ãã"}
format_new_magazine GET /magazines/new.:format {ïŒcontroller => "magazines" ,: action => "new"}
edit_magazine GET / magazines /ïŒid / edit {ïŒcontroller => "magazines" ,: action => "edit"}
formatted_edit_magazine GET /magazines/:id/edit.:format {ïŒcontroller => "magazines" ,: action => "edit"}
magazine GET / magazines /ïŒid {ïŒã³ã³ãããŒã©ãŒ=> "éèª"ãïŒã¢ã¯ã·ã§ã³=> "ã·ã§ãŒ"}
format_magazine GET /magazines/:id.:format {ïŒcontroller => "magazines" ,: action => "show"}
PUT / magazines /ïŒid {ïŒcontroller => "magazines" ,: action => "update"}
PUT /magazines/:id.:format {ïŒcontroller => "magazines" ,: action => "update"}
DELETE / magazines /ïŒid {ïŒcontroller => "magazines" ,: action => "destroy"}
DELETE /magazines/:id.:format {ïŒcontroller => "magazines" ,: action => "destroy"}
ãµãŒããŒãèµ·åããŠããã°ãåçããŸãã ãã ããæåã«ããŒã¿ããŒã¹ãäœæãã移è¡ãéå§ããŸãã
rake db:create
rake db:migrate
./script/server
ç§ãã¡ã®éèªã¯
ããŒã«ã«ãã¹ãã§å©çšå¯èœã«ãªããŸããïŒ3000 / magazines
ããã€ãã®æ°ããèšäºãäœæããŸãã
äžèšã®ã«ãŒãããŒãã«ã«æ»ããŸãã æåã®åã¯ãroutesãšããååã§ãã 圌ãã¯éåžžã«å¿«é©ã§ãã æ°ããèšäºãäœæããããã®ãªã³ã¯ãäœæããããã®ãªãã·ã§ã³ãããã€ããããŸãã app / views / magazines / index.html.erbãã¡ã€ã«ãéããŸã-ããã¯ãmagazines_controllerã³ã³ãããŒã©ãŒã®indexã¡ãœããã®ãã¥ãŒã§ãã
äžéšã«ãã³ãŒããè¿œå ããŸãããã
<hr> <%= link_to ' ', :action => 'new' %><br /> <%= link_to ' ', '/magazines/new' %><br /> <%= link_to ' ', new_magazine_path %><br /> <%= link_to ' ', new_magazine_url %><br />
æãæ£ããã®ã¯ãæåŸã®2ã€ã®æ¹æ³ã䜿çšããããšã§ãã éãã¯ãurlã¯å®å
šãªãªã³ã¯ïŒhttpïŒ// localhostïŒ3000 / magazines / newïŒãè¿ããpathã®ã¿ããã¹ïŒ/ magazines / newïŒãè¿ãããšã§ãã ååä»ãã«ãŒãã䜿çšããæ¹ãè¯ãã®ã¯ãªãã§ããïŒ ååä»ãã«ãŒãã¯ããã®ã«ãŒãã䜿çšãããã¹ãŠã®ãªã³ã¯ãå€æŽããããšã«ãã£ãŠå€æŽãããå€æ°ã§ãã æã§ãã¹ãæžãããšã¯ãŸã£ãããå§ãã§ããŸããããæžãå Žåã¯action => 'new'ïŒå€ãã®å Žåããã¹ãŠã®å Žåã«ååãªååä»ãã«ãŒãããªãããããã®ãªãã·ã§ã³ã¯éåžžã«äžè¬çã§ãïŒ
ããŒãã«ã®2åç®ã¯ã¯ãšãªã¡ãœããã§ãã ãŸã£ããåããªã³ã¯ã§ãããã¡ãœãããç°ãªããšãã³ã³ãããŒã©ãŒã®ã¡ãœãããç°ãªããŸãã ããšãã°ãåãã¢ããª/ãã¥ãŒ/éèª/index.html.erbã§ïŒ
<%= link_to 'Show', magazine %>
<%= link_to 'Destroy', magazine, :method => :delete %>
æåã®ãªã³ã¯ã§ã¯ãããã©ã«ãã®GETèŠæ±ãå®è¡ããïŒlinkïŒmethod =>ïŒgetã§ãªã³ã¯ãçç¥ã§ããŸãïŒã2çªç®ã§ã¯methodïŒdeleteãéä¿¡ãããŸãã ãŸããã©ã¡ãã®å Žåãéèªã®ãªã³ã¯ã¯åããŸãŸã§ãã
3çªç®ã®åã¯ãå®éã«ã¯HTMLã§ååŸãããªã³ã¯ã§ãã æåŸã®åã¯ãã³ã³ãããŒã©ãŒãšã¡ãœãããžã®ãªã³ã¯ã®å¯Ÿå¿ã§ãã ãã§ã«äžã§è¿°ã¹ãããã«ãä»»æã®ãªã³ã¯ã¯ãã³ã³ãããŒã©ãã¢ã¯ã·ã§ã³ïŒããã³å Žåã«ãã£ãŠã¯ã¡ãœããïŒã®ãã¢ãšããŠè¡šãããšãã§ããŸãã
<ïŒ
= link_to 'ãã¬ãžã³ããå¥ã®ã³ã³ãããŒã©ãŒãžã®ãªã³ã¯'ãïŒcontroller => 'blogs' ,: action => 'show' ,: id => '1' ,: method => 'GET'ïŒ
>
ãããã£ãŠãã€ã³ããã¯ã¹1ã®ããã°ãžã®ãªã³ã¯ãååŸããŸãïŒããã§ã¯ãã¡ãœããgetãæå®ã§ããŸããã§ããïŒã
<ïŒ
= link_to 'ã³ã³ãããŒã©ãŒã®ãã€ãã£ãã¡ãœãããžã®ãªã³ã¯'ãïŒaction => 'show' ,: id => '1'ïŒ
>
ã€ã³ããã¯ã¹1ã®èšäºãžã®ãªã³ã¯ããã®å Žåãã³ã³ãããŒã©ãŒãšHTTPã¡ãœãããæå®ããå¿
èŠã¯ãããŸãããGETãããã©ã«ãã§å®è¡ãããæå®ããªãå Žåã¯ã³ã³ãããŒã©ãŒãåãããã«å®è¡ãããããã§ãã
config / routes.rbãã¡ã€ã«ãéããŸãïŒã³ã¡ã³ãåãããããã¹ãã¯ãã¹ãŠåé€ã§ããŸãïŒ
map.resources :magazines
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
scaffoldã³ãã³ãã¯æåã®è¡ãæ¿å
¥ããŸããã ãã®è¡ã«ãããäžèšã§ç¢ºèªããäžé£ã®ã«ãŒããè¿œå ãããŸããã
localhostïŒ3000ãšå
¥åãããšãããããããŒãžã衚瀺ãããŸãã ãããä¿®æ£ããŸãããã
map.root :controller => 'magazines'
ããã§ããããªãã¯ãã©ã«ããŒããindex.htmlãåé€ãã
localhost ïŒ3000ã«ç§»åãããšãç®çã®å Žæã«çŽæ¥ç§»åã§ããŸã=ïŒã ããã«ããã¹ãŠã®ã¬ãŒãã«ãŒããèŠããšãæ°ããååä»ãã«ãŒãã«ãŒãã衚瀺ãããŸãã ãŸããã¡ãã¥ãŒã§ãã¡ã€ã³ããã¥ãŒãžã®ãªã³ã¯ãäœæã§ããŸãã
<%= link_to '', root %>
ãããŠããªã³ã¯ãæãªãããšãªãããã€ã§ãããŒã ããŒãžãmap.rootã¹ãã¢ã«å€æŽã§ããŸãïŒcontroller => 'shop'
IIã¬ãã«
å®éã«ã«ãŒããäœæããããèªåã®æã§æåã®ååä»ãã«ãŒããäœæããŸããã
ååä»ãã«ãŒãã
localhost ïŒ3000 / zhurnalããäœæããŸãããã ãã«ãžã§ã¢ã®ãéèªãã欲ããã®ã§ã¯ãªãããã·ã¥ã«ãã«ãã欲ããïŒ
map.zhurnal '/klevi_zhurnal/:id', :controller => 'magazines', :id => nil
ããã§ãååä»ãã®zhurnalã«ãŒããäœæããŸããããã®URLã¯
localhost ïŒ3000 / klevi_zhurnalã®ããã«ãªãããã¬ãžã³ã³ã³ãããŒã©ãŒããã³ã³ãã³ããåãåããŸãã
localhost ïŒ3000 / klevi_zhurnal / 1ã®ãããªèšäºãä»ããèªã¿èŸŒãããšãããšããããç ŽããŸãã ã«ãŒãã«ããã€ãã®å€æŽãå ããŸãããã
map.zhurnal '/klevi_zhurnal/:action/:id', :controller => 'magazines', :action => 'index', :id => nil
ããã¯ãã¹ãŠã©ãããæå³ã§ããïŒ
-/ klevi_zhurnal /ãšãã圢åŒã®URLã解決ãããŸãïŒcontroller => 'magazines' ,: action => 'index' ,: id => 'nil'-ã€ãŸããã€ã³ããã¯ã¹ããŒãžïŒindex.html.erbïŒãååŸããŸã
-/ klevi_zhurnal / 1ã¯ãã¢ã¯ã·ã§ã³ '1'ãååšããªããšãããšã©ãŒãåãåºããŸãïŒã«ãŒãã§åŒæ°ãæž¡ãã·ãŒã±ã³ã¹ãèŠãŠãã ããïŒ
-/ klevi_zhurnal / showã¯idãæå®ãããŠããªããšèšããŸã
-/ klevi_zhurnal / show / 1-ID = 1ã®èšäºãæäŸããŸãïŒç¢ºãã«ååšããå ŽåïŒ
-/ klevi_zhurnal / edit / 1-ãã®èšäºã®ç·šéãã©ãŒã ãæäŸããŸã
確ãã«ããªã³ã¯èªäœã¯å°ãéããªããŸããã
<ïŒ
= link_to 'ãã¹ãŠã®èšäº'ãmagazines_pathïŒ
>ã®ä»£ããã«ã<ïŒ
= link_to 'ãã¹ãŠã®èšäº'ãzhurnal_pathïŒ
>ããããŸãã
<ïŒ
= link_to 'Article number 1'ãmagazine_pathïŒ '1'ïŒïŒ
>ã®ä»£ããã«<ïŒ
= link_to 'Article number 1'ãzhurnal_pathïŒïŒaction => 'show' ,: id => '1'ïŒïŒ
>
ã«ãŒãã®ç解ãæ·±ããããã«ãè€æ°åœ¢/åæ°åœ¢ã·ã¹ãã ãå°å
¥ãããŠããããšã«æ³šæããŠãã ããã
ãã¹ãŠã®èšäºmagazines_pathã衚瀺ãã
åå¥ã®èšäºã衚瀺ïŒmagazine_pathã
ãããŒ-æ£ããèšèã§ã¯ãããŸãã=ïŒã ArticleãšåŒã°ãããã®ããã¹ãŠããå ŽåïŒ
ã€ã³ããã¯ã¹=> articles_pathãshow => article_pathïŒïŒidïŒ
ããã§ã¯ãæ°ããã¡ãœãããäœæããŸãããã
ã¢ããª/ã³ã³ãããŒã©ãŒ/ magazines_controller.rbãéããŸã
ããã«ã¡ãœãããè¿œå ããŸã
def random
offset = rand(Magazine.count :all)
@magazine = Magazine.find(:first, :offset => offset)
render :action => 'show'
end
ãã®ã¡ãœããã¯ãã©ã³ãã ãªèšäºãè¿ãã ãã§ãã åŒã³åºããŠã¿ãŸãããïŒ
localhost ïŒ3000 / magazines / random
ãšã©ãŒãçºçããŸã-ããã«ã¯ç§ãã¡ããã®IDãå¿
èŠã§ãã ãªãã§ïŒ æšæºã«ãŒãã¯æ¬¡ã®åœ¢åŒã®ã«ãŒããæå³ããããïŒcontroller /ïŒaction /ïŒidã
ã«ãŒã«ã«åŸã£ãŠã«ãŒããåŒã³åºããŠã¿ãŸãããïŒ
ããŒã«ã«ãã¹ã ïŒ3000 / magazines / random / 1230492
ãã®IDã®ã¬ã³ãŒãã¯ååšããŸããããæ©èœããŸãïŒ ã¡ãœããã§ã¯IDããŸã£ãã䜿çšããªããããããã«ã©ã®ãããªãã³ã»ã³ã¹ãæžããã¯éèŠã§ã¯ãããŸããã
ããã§ãã
localhostãšãã圢åŒã®æ£ããã«ãŒããäœæããŠã¿ãŸãããïŒ3000 / magazines / random /
ããã«ã¯ãªãã·ã§ã³ããããŸãïŒcollection => {ïŒaction =>ïŒHTTP_method}
ç§ãã¡ïŒã¢ã¯ã·ã§ã³ã¯ïŒã©ã³ãã ãã¡ãœããã¯ïŒget
ç§ãã¡ã¯åŸã
map.resources :magazines, :collection => { :random => :get }
ä»ã§ã¯ãã¹ãŠãæ©èœããŸãïŒ =ïŒ
ããã§çŽ¹ä»ã¯çµããã§ãã åé¯ã®ããæŽç·Žãããæ¹æ³ãç§ãã¡ãåŸ
ã£ãŠããŸãïŒã ããããä»æ¥ã§ã¯ãããŸããã
èšäºãèªãã§ãããŠããããšãã
ãåžæã®æ¹ã®ããã®APIã©ã€ã¢ã³ã»ãã€ãã«ããã¹ã¯ãªãŒã³ãã£ã¹ã