ã³ãŒãã®æ¹åãšæé©åã«ã¯ãæåã®ããŒãžã§ã³ãäœæãããããæéããããå ŽåããããŸãã æ°ããã³ãŒããèå³æ·±ãå®è£ ã«é »ç¹ã«äŒã£ãŠãèªåã«ããèšããŸããïŒãããã¯æšæºçãªæ段ã䜿ã£ãŠã§ããããšãããã£ãããç§ã¯èªè»¢è»ãçºæãããïŒ å人çã«ã¯ããã§ãã ãããã£ãŠããã®èšäºã§ã¯ãèªè»¢è»ãã®ã³ã¬ã¯ã·ã§ã³ããŸãšããããããåãé€ãæ¹æ³ã説æããŸããã
é åã¡ãœãã
ç°¡åãªäŸ-Userã¯ã©ã¹ã®ãªããžã§ã¯ãã®é åããããšããŸãããã ãŠãŒã¶ãŒã«ã¯ãã¢ã¯ãã£ãåãããããããã£ãããããŠãŒã¶ãŒãã¢ã«ãŠã³ããã¢ã¯ãã£ãåããå Žåã¯1ã«èšå®ãããŸãã ã¢ã¬ã€ã®ãã¹ãŠã®ãŠãŒã¶ãŒãã¢ã¯ãã£ãåãããŠãããã©ããã確èªããå¿ èŠããããŸãã ActiveRecordãèæ ®ããŠããŸããïŒå¥ã®æ¹æ³ããããŸãïŒãç§ã®ç®æšã¯é åã®æäœæ¹æ³ã瀺ãããšã§ãã
æåã®æ¹æ³ãæãåå§çãªïŒ
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
@users = User. find ( :all ) activated_users = 0 foreach user in @users activated_users + = 1 if user. activated == 1 end # , activated_users == @users . size
ããã§æåã«ããããšã¯ãã«ãŒãã§ã¯ãªãåé åã¡ãœããã䜿çšããããšã§ããã
ãããããã®èšé²ã§ããåŸã§ç§ã«åããªãã£ãã ããåççãªãªãã·ã§ã³ãèŠã€ããŸããïŒ
- @users =ãŠãŒã¶ãŒã æ€çŽ¢ ïŒ ïŒãã¹ãŠ ïŒ
- activate_users = 0
- ïŒç§ã¯å人çã«ãã®èšé²æ¹æ³ã奜ãã§ãã
- ïŒå€ãã®èšèªã®æšæºforeachã«ãŒããã
- @users ãããã ã | ãŠãŒã¶ãŒ|
- activate_users + = 1ãŠãŒã¶ãŒã®å Žåã æå¹å == 1
- çµãã
- ïŒãŸãã¯1è¡ã§ïŒ
- @users å { | ãŠãŒã¶ãŒ| activate_users + = 1ãŠãŒã¶ãŒã®å Žåã æå¹å == 1 }
ãã ãããã¹ãŠã®åªãããã®ããã§ã«çºæãããŠããã®ã§ãããã¥ã¡ã³ããèªãã§ãã ããã é åã«ã¯çŽ æŽããããã¹ãŠããããŸããïŒãã¹ãŠã®èŠçŽ ãæ¡ä»¶ãæºãããã©ããã確èªããã¡ãœããã ãšããã§ãå°ãªããšã1ã€ã®èŠçŽ ãæ¡ä»¶ã«äžèŽããå Žåã«trueãè¿ãanyïŒã¡ãœããããŸã ãããŸãã
- ïŒselectã¡ãœããã䜿çšãããšãç¹å®ã®æ¡ä»¶ã«åŸã£ãŠé åã®èŠçŽ ãåŒãåºãããšãã§ããŸã
- ïŒãããŠãäž¡æ¹ã®é åã®ãµã€ãºãæ¯èŒããŸã
- @users =ãŠãŒã¶ãŒã æ€çŽ¢ ïŒ ïŒãã¹ãŠ ïŒ
- @users { | ãŠãŒã¶ãŒ| ãŠãŒã¶ãŒ ã¢ã¯ãã£ããŒã == 1 } ã ãµã€ãº == @users ã ãµã€ãº
ã³ãŒãã¯ãã¹ãŠ1è¡ã§èšè¿°ã§ããŸãã
- @users ãã¹ãŠ ïŒ { | ãŠãŒã¶ãŒ| ãŠãŒã¶ãŒ æå¹å == 1 }
- ïŒãŸãã¯ãã®ããã«ïŒ
- @users ãã¹ãŠ ïŒ ïŒ ïŒ ïŒactivate ïŒ ïŒactivateãtrue / falseã«èšå®ãããŠããå Žåã Rubyã®0ã¯trueãšèŠãªãããŸã
ããã«ãããŸãç¥ãããŠããªãã䟿å©ãªæ¹æ³ãããã€ããããŸãã
- ïŒæåã®5ã€ã®èŠçŽ ãååŸãã
- @array ã åã ïŒ 5 ïŒ
- ïŒã©ã³ãã ãªã¢ã€ãã ãéžæ
- @array ã éžæ
- ïŒã¢ã€ãã ãã©ã³ãã ã«æ£åžãã
- @array ã ã·ã£ããã« ïŒ
- ïŒçªå·ã§é åèŠçŽ ãååŸ
- ïŒããã«ããã®ã¡ãœããã¯@array [1]ãããé«éã«åäœããŸã
- @array ã ïŒ 1 ïŒ
é åå ã®èŠçŽ ã®æ°ãæ°ããæè¯ã®æ¹æ³ã¯äœã§ããïŒ
ã«ãŠã³ããæãæ©èœçã§ãã æ¬åŒ§å ã®ãã©ã¡ãŒã¿ãŒãæå®ã§ããŸãã
- é å= [ 1ã1ã2ã2ã3ã4ã5 ]
- é å ã«ãŠã³ã ïŒ=> 7
- é å count ïŒ 1 ïŒ ïŒèŠçŽ ã®æ°ãã«ãŠã³ãããŸã "1" => 2
- é å ã«ãŠã³ã { | p | p > 2 } ïŒæ¡ä»¶ããšã«èŠçŽ ãã«ãŠã³ãããŸã=> 3
ãµã€ãºãŸãã¯é·ã ïŒé åã§åãïŒã¯ããã¹ãŠã®èŠçŽ ã®æ°ãåçŽã«ã«ãŠã³ãããããšã ããç®çãªã®ã§ãæãéãæ¹æ³ã§ãã
é »ç¹ã«äœ¿çšãããActiveRecordã¯ãšãª
ç§ãåºäŒã£ããã1ã€ã®ããã€ã¯ãã¯ã ååä»ãã¹ã³ãŒãã®ç¬èªã®å®è£ ã§ãã ããšãã°ãCarsããŒãã«ããããŸãã å€ãã®å Žåãåºå°ã«è¡ããç¹å®ã®è²ã®è»ãåŒãåºãå¿ èŠããããŸãã ã¢ãã«ã«ç¬èªã®ã¡ãœãããæžãããšãã§ããŸãïŒ
圌ã¯ãã¹ãŠã®èµ€ãè»ãæ¢ããŠããŸãã 圌ã«ç®ãåããã®ã¯ç°¡åã§ã-Car.only_redã ããããäž»ãªæ¬ ç¹ã¯ããã¹ãŠãæŽçããå ŽåïŒãŸãã¯ãlimit => 5ãªã©ãè¿œå ããå ŽåïŒãã¢ãã«å ã®ã¡ãœãããå€æŽããããæšæºã®findã䜿çšããå¿ èŠãããããšã§ãã
- ã¯ã©ã¹ Car < ActiveRecord :: Base
- ããã»ã«ã ã only_red
- èªå·± ã find ïŒ ïŒall ãïŒconditions => "color = 'red'" ïŒ
- çµãã
- çµãã
éåžžã«äŸ¿å©ã§äœ¿ããããnamed_scopeãå©ãã«ãªããŸãã
- ã¯ã©ã¹ Car < ActiveRecord :: Base
- named_scope ïŒèµ€ ãïŒ æ¡ä»¶ => 'è²= "èµ€"'
- çµãã
Car.red-ã»ãŒåæ§ã«äœ¿çšããŸãã ãã ããããã«å ããŠãfindã¡ãœãããšçµã¿åãããŠäœ¿çšââããããšãã§ããŸããäŸïŒ
ãããŠãããããçµã¿åãããããšãã§ããŸãã æåã«ããã·ã³ãææãããŠãŒã¶ãŒããªã¯ãšã¹ãã«å«ããããšãã§ããæ°ããã¹ã³ãŒããè¿œå ããŸãã
- è»ã èµ€ ã find ïŒ ïŒall ãïŒlimit => 10 ãïŒorder => "id DESC" ïŒ
- named_scope ïŒwith_users ,: include => ïŒusers
ããã§Car.red.with_usersãããŠããŠã¯ãšãªãäœæããŸãã 䟿å©ã§ããïŒ ééããªãã
Link_toãšåœŒã®åé
æãäžè¬çãªlink_toãªã³ã¯ã«ã¯ãããã«èå³æ·±ãé¡äŒŒç©ãããã€ããããŸãã
1ã€ç®ã¯link_to_ifã§ãæ¡ä»¶ã«äžèŽããå Žåã«ã®ã¿ãªã³ã¯ãåºåããŸãã äžèŽããªãå Žåããªã³ã¯ã¯ãªããããã¹ãã®ã¿ããã®å Žæã«æ®ããŸãã
ããšãã°ããã«ã«ããäžããããšãããªã³ã¯ããããŸãã ãŠãŒã¶ãŒããã§ã«æ祚ããŠããå Žåã¯ãå©çšã§ããŸããã ãã®ç®çã®ããã«å¥ã®ã¹ã¿ã€ã«ãäœæãããªã³ã¯ããã¬ãŒã³ããã¹ããšããŠè¡šç€ºãããã®ããããŸãã ããããããªãã¯æžãããšãã§ããŸãïŒ
ããã§äœãå€æŽã§ããŸããïŒ ãã¡ãããïŒuser.voted_for_karmaïŒ== falseïŒã¯æ確ã«èŠããŸãããlink_to_unlessã䜿çšããŠfalseãšã®æ¯èŒãç°¡åã«åé€ã§ããããã§ãã
- link_to_if ïŒ userãvoted_for_karma ïŒ== false ïŒ ã "ã«ã«ããäžãã "ãïŒ action => "add_karma"
ããã«ã¯ãŒã«ã«ããããšãã§ããŸã-ãŠãŒã¶ãŒãæ¢ã«æ祚ããŠãããšããŸãããã
- link_to_unlessãŠãŒã¶ãŒã voted_for_karma ïŒã "Raise Karma "ãïŒ action => "add_karma"
ãŸãããŠãŒã¶ãŒããã°ã€ã³ããŠããªãå Žåãããšãã°ç»é²ã«ãªãã€ã¬ã¯ããããªã©ãå¥ã®ãªã³ã¯ãæå®ããããšãã§ããŸãã
- ïŒããã§link_nameã¯å ã®ãªã³ã¯ã®ååãšããŠäœ¿çšãããŸãïŒäœ¿çšãããŸããïŒ
- link_to_unless ïŒ userãvoted_for_karma ïŒã "Raise karma "ãïŒ action => "add_karma" ïŒ { | link_name | ãããªãã¯ãã§ã«æ祚ããŠããŸãã }
- link_to_if ïŒ userãlogged_in ïŒã "ãããã¯ã®äœæ "ãïŒ action => "add_post" ïŒ do | link_name |
- ïŒãã°ã«èšé²ãããŠããªããŠãŒã¶ãŒããªã³ã¯ãã¯ãªãã¯ãããšããã°ã€ã³ããŒãžã«ç§»åããŸã
- link_to ïŒ link_name ,: controller => "users" ãïŒ action => "login" ïŒ
- çµãã
ãã©ãŠã¶ãŒãçŸåšã®ããŒãžã«ããå Žåããªã³ã¯ã®ããã¹ããžã®çœ®æãèŠãããšãã§ããŸãã ããã¯link_to_unless_currentã䜿çšããŠè¡ãããŸãïŒ
- <ïŒ-index.html.erb->
- < ul id = "navbar" >
- < li > <ïŒ = link_to_unless_current ïŒ "Home" ã { ïŒ action = > "index"}ïŒïŒ > < / li >
- < li > <ïŒ = link_to_unless_current ïŒ "About Us" ã { ïŒ action = > "about"}ïŒïŒ > < / li >
- < / / ul >
- <ïŒ-About UsããŒãžã«ç§»åãããšãhtmlã³ãŒãã«æ¬¡ã®ããã«è¡šç€ºãããŸãã->
- < ul id = "navbar" >
- < li > <a href = "/ controller / index">ããŒã < / a > < / li >
- < li >äŒç€ŸæŠèŠ< / i >
- < / / ul >
æ€èšŒã®ãã·ã¢å
ç§ããã€ãŠééããå¥ã®åé¡ã¯ãæ€èšŒãšãšã©ãŒã§ãã ç§ã®ã¢ããªã±ãŒã·ã§ã³ã¯ïŒãã¡ããïŒãã·ã¢èªã§ãããã©ã«ãã§ã¯æ¬¡ã®åœ¢åŒã§ãšã©ãŒãçºè¡ãããŸããïŒãã¿ã€ãã«ã空ã«ããããšã¯ã§ããŸãããã
ãŸãããã·ã¢ã®gemãã€ã³ã¹ããŒã«ããå¿ èŠããããŸããããã«ã€ããŠæ¢ã«ç¥ã£ãŠããããšãæ¬åœã«é¡ã£ãŠããŸãã ããã«ãããã»ãšãã©ã®æšæºã¡ãã»ãŒãžãããŒã«ã©ã€ãºãããŸãã
次ã«config / localesãã©ã«ããŒã§ru.ymlãã¡ã€ã«ãéãïŒãŸãã¯äœæããªãå Žåã¯äœæããïŒå¿ èŠããããŸãã ãããç§ã®ãã¡ã€ã«ã®äŸã§ãïŒ
ã芧ã®ãšããããã¥ãŒã¹ããŒãã«ã®ã¿ã€ãã«ãã£ãŒã«ããããŒã«ã©ã€ãºããã«ã¯ãäžèšã®ã¬ã³ãŒããããŒã«ã©ã€ãºãã¡ã€ã«ã«è¿œå ããŸãã
- ruïŒ
- activerecordïŒ
- ãšã©ãŒ ïŒ
- full_messagesïŒ
- ã¬ãŒãïŒ
- existsïŒ ãåããŠãŒã¶ãŒã2åè©äŸ¡ããããšã¯ã§ããŸããã
- number_limitïŒ ãè©äŸ¡ã¯-2ãã2ãŸã§ã®å€ãåãããšãã§ããŸãã
- å±æ§ïŒ
- ãã¥ãŒã¹ïŒ
- ã¿ã€ãã«ïŒ ãã¿ã€ãã«ã
- æ¬æïŒ ãããã¹ãã
- èšäºïŒ ãèšäºã
full_messagesãã£ãŒã«ãã¯ãå®å šãªãšã©ãŒã¡ãã»ãŒãžã«äœ¿çšãããŸãã ã¢ãã«ãžã®é©åæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
messageãã©ã¡ãŒã¿ãŒã¯ããŒã«ã©ã€ãºãã¡ã€ã«ãååŸããã¢ãã¬ã¹activerecord.errors.full_messagesã«ç§»åããŠãããã¢ãã«ã§æå®ãããå Žæã«ç§»åããããŒã«ã©ã€ãºãããæååãè¿ããŸãã ãã¹ãŠãéåžžã«ç°¡åã§ãã ã¡ãã»ãŒãžãæå®ããªãå Žåãåºåã¯æšæºã®ãšã©ãŒãã¬ãŒãºã«ãªããŸãããç§ã®å Žåã¯ããããç§ã®ãã®ã«çœ®ãæããŸããã
- ã¯ã©ã¹ Rate < ActiveRecord :: Base
- validates_uniqueness_of ïŒrate_owner_id ,: message => "rate.exists"
- validates_inclusion_of ïŒvalue ãïŒ in => [ -2 ã -1ã1ã2 ] ãïŒ message => "rate.number_limit"
- çµãã
ã³ã³ãããŒã©ãŒã®ã¡ãœãã
ajaxãšæ§åŒã®äž¡æ¹ã®æ¹æ³ã§ã¢ã¯ã·ã§ã³ã§ããŒã¿ãè¿ãå¿ èŠãããå Žåãrequest.xhrïŒã䜿çšããŠç¢ºèªã§ããŸãã次ã«äŸã瀺ããŸãã
å¥ã®äŸ¿å©ãªæ¹æ³ã¯æ€èšŒã§ãã ååããå€æãããšãæ£ããèŠæ±ãã³ã³ãããŒã©ãŒã«å±ããããšã確èªããã®ã«åœ¹ç«ã¡ãŸãã ããã¯äž»ã«ä¿è·ã®ããã§ãããäžéšã®æ¹æ³ã§ã¯äœ¿çšããããšã匷ããå§ãããŸãã
- defæ€çŽ¢
- @users =ãŠãŒã¶ãŒã find ïŒ ïŒall ãïŒconditions => "login LIKE 'ïŒ{params [ïŒq]}'"ãïŒ limit => 30 ïŒ
- render ïŒjson => @users if request xhr ïŒ
- çµãã
ãšããã§ãå¯èœãªéã詳现ã«ãšã©ãŒããã€ã³ãããã¹ãã§ã¯ãããŸãã-äž»ãªæ€èšŒã¯ã¢ãã«ã§è¡ãããã¹ãã§ãã ããã§ãã©ã¡ãŒã¿ããã§ãã¯ããäœãåé¡ãããå Žåã¯èªããŸãã
- ã¯ã©ã¹ UsersController < ApplicationController
- ïŒcreateã¡ãœããã確èªããŸã-postèŠæ±ãšæå®ããããã©ã¡ãŒã¿ãŒã®ã¿ãåãå ¥ããå¿ èŠããããŸãã
- ïŒäœãåé¡ãããå Žåã¯ããŠãŒã¶ãŒãå é ã«ãªãã€ã¬ã¯ããããã©ãã·ã¥ãšã©ãŒã¡ãã»ãŒãžãè¿œå ããŸãã
- verify ïŒonly => ïŒcreate ãïŒ method => ïŒpost ãïŒparams => [ ïŒlogin ãïŒemail ,: name ] ãïŒ redirect_to => ïŒindex ãïŒadd_flash => { ïŒerror => 'Invalid requestã 倧äžå€«ã§ããïŒã }
- çµãã
ãããã°
ã¢ããªã±ãŒã·ã§ã³ã®ãããã°ã«ã¯ããã¬ãŒãšruby-debug gemã䜿çšããŸãã ãã¬ãŒã¯ãããŠãŒã¶ãŒã®ä»£ããã«åœŒãäœãè¿ãã®ãããç°¡åã«ãã°ããèŠã€ããæ¹æ³ã§ãã ããã¯åçŽã«è¡ãããŸãïŒ
ãšããã§ãlogger.debugã ãã§ãªããlogger.infoãlogger.warnããŸãã¯logger.fatalã䜿çšããããšãã§ããŸãã RAILS_DEFAULT_LOGGER.debugã¯ãlogger.debugãšãããã¬ãŒãºã«å¿çããªããããç¬èªã®ã¯ã©ã¹ã§äœæã§ããŸãã
- ïŒã³ã³ãœãŒã«ã«ãªããžã§ã¯ãã䟿å©ã«è¡šç€ºããããã«åŒã³åºã
- ãã¬ãŒã debug "users =ïŒ{users.inspect}"
å€æ°ã®ç¶æ ã確èªãŸãã¯å€æŽããå¿ èŠãããå Žåã¯ããããã¬ãŒã䜿çšã§ããŸãã ãããè¡ãã«ã¯ãããšãã°æ¬¡ã®ããã«ãããã¬ãšã³ããªãé 眮ããŸãã
ãã®åŸã-debuggerãã©ã¡ãŒã¿ãŒã§ãµãŒããŒãèµ·åããããè¿œå ããå¿ èŠããããŸã
- ã¯ã©ã¹ UsersController < ApplicationController
- defã€ã³ããã¯ã¹
- @user =ãŠãŒã¶ãŒã find_by_id ïŒ params [ ïŒid ] ïŒ
- role = @user ã 圹å²
- ãããã¬ãŒ
- çµãã
- çµãã
environment.rbã§ã ãã®åŸããã©ãŠã¶ã§ã€ã³ããã¯ã¹ããŒãžãæŽæ°ããŸãããåæ¢ããããŒããããŸããã ã³ã³ãœãŒã«ãèŠãæãæ¥ãŸãããããã§ãããã¬ãåŸ ã£ãŠããŸãã
- ãruby-debugã ãå¿ èŠ
- ïŒrdbïŒ1ïŒirb
- irbïŒïŒ<UsersControllerïŒ0x1039edd20>ïŒïŒ001ïŒ0> @user
- =>ïŒ<ãŠãŒã¶ãŒIDïŒ1ããã°ã€ã³ïŒ "Vizakenjack">
- irbïŒïŒ<UsersControllerïŒ0x1039edd20>ïŒïŒ001ïŒ0> @ user.login = 'Admin'
- =>ã管çè ã>
ãŸãirbã«é²ã¿ã次ã«å€æ°ãèŠãŠãã ããã 衚瀺ã«å ããŠãããããå€æŽããŠãããã¢ããªã±ãŒã·ã§ã³ã«å ¥ããããšãã§ããŸãã
ãããã¬ã«é¢ããæ¬å šäœãæžãããšãã§ããŸããããã§ã¯ãåºæ¬çãªäœ¿çšäŸã®ã¿ã瀺ããŸããã ããã¹ãŠãäžåºŠã«ãããã人ã®ããã«-以äžã®æ®µèœã®ãªã³ã¯ãèªãã§ãã ããã
ãããã«
ããã§ãç§ãå©ããŠãããããã€ãã®äŸ¿å©ãªãªã³ã¯ãæäŸããŸãããããŠãããªãã«ã圹ç«ã€ããšãé¡ã£ãŠããŸãã
- ã¬ãŒã«äžã®ã¬ã€ã ã è±èªã§ éåžžã«äŸ¿å©ãªèšäºã®ã³ã¬ã¯ã·ã§ã³ã
- Ruby Toolbox-ãã©ã°ã€ã³ãå®ç³ãããã³æ¡åŒµæ©èœã®ã³ã¬ã¯ã·ã§ã³ã ãã¹ãŠã®æ©äŒã«ã
- Rails APIããã³Ruby API èšèªãšãã¬ãŒã ã¯ãŒã¯ã®ããã¥ã¡ã³ãã å€ãã®é¡äŒŒç©ããããŸãããç§ã¯ãã®ãµã€ãã奜ãã§ããã
- ãã·ã¢èªã®Ruby Wikibook ã ãšãŠãèªã¿ãããã§ãã èå³ã®ããæ¹ã¯ãããããã¯ãŒã¯ãã»ã¯ã·ã§ã³ã«ã¢ã¯ã»ã¹ããŠãã ãã-ããã€ã®æšéŠ¬ã®èª¬æããããŸãïŒ
- Rubularã¯ãæ£èŠè¡šçŸãäœæããã³ãã¹ãããããã®ãµã€ãã§ãã