scaffoldãå©ç¨ããRailsã¢ããªã±ã¼ã·ã§ã³ã®åºæ¬åä½ç¢ºèªï¼ç¶ãï¼ï¼1ï¼
ãååã¾ã§ã¯scaffoldã§èªåçæããã½ã¼ã¹ã³ã¼ãã®ãã¡ãã«ã¼ãã£ã³ã°ã確èªãã¾ãããä»åããã¯èªåçæãããUsersã³ã³ããã¼ã©ã¼ã®åã¢ã¯ã·ã§ã³ããå®éã®åä½ã¨è¦æ¯ã¹ãªãã解説ãã¾ãã
Usersãªã½ã¼ã¹ä¸è¦§ï¼indexã¢ã¯ã·ã§ã³ï¼
ãã¾ãUsersãªã½ã¼ã¹ä¸è¦§ãã¤ã¾ãuserã³ã³ããã¼ã©ã¼ã®indexã¢ã¯ã·ã§ã³ã¸ã¢ã¯ã»ã¹ããã¾ããå ã»ã©ã®å®è¡çµæã®URIã確èªããã¨URIã¯/usersã§ãã®ã§ãhttp://localhost:3000/usersã¸ã¢ã¯ã»ã¹ãã¾ãã以ä¸ã®ãããªç»é¢ã表示ãããã¯ãã§ãã
ãã¢ã¯ã»ã¹ããã¨åããéããã¾ã usersãã¼ãã«ã«ã¯ä½ãå ¥ã£ã¦ããã空ãªã®ã§ãUsersãªã½ã¼ã¹ã«é¢ããã¬ã³ã¼ãã¯ä½ã表示ããã¾ãããããã§å¯¾å¿ããusersã³ã³ããã¼ã©ã¼ã®ã½ã¼ã¹ã³ã¼ãã確èªãã¾ãã
class UsersController < ApplicationController â¦ï¼ä¸ç¥ï¼â¦ def index @users = User.all end â¦ï¼ä¸ç¥ï¼â¦
ãindexã¡ã½ããã追å ããã¦ãããActiveRecordã®allã¡ã½ããã使ç¨ãã¦å ¨ã¦ã®Userã¢ãã«ã@usersã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ä»£å ¥ãã¦ããã³ã¼ããèªåçæããã¦ãã¾ããã³ã³ããã¼ã©ã¼ã§ã¢ãµã¤ã³ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¯ãã¥ã¼å´ã§å©ç¨ãããã¨ãã§ããããã«ãªãã¾ãã
ã対å¿ãããã¥ã¼å´ã®ã½ã¼ã¹ã³ã¼ãã確èªãã¾ãã
â¦ï¼ä¸ç¥ï¼â¦ <tbody> <% @users.each do |user| %> <tr> <td><%= user.name %></td> <td><%= user.email %></td> <td><%= link_to 'Show', user %></td> <td><%= link_to 'Edit', edit_user_path(user) %></td> <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> â¦ï¼ä¸ç¥ï¼â¦
ããã¥ã¼ãã¡ã¤ã«ã§@usersãeachã§ã«ã¼ããã¦userã¬ã³ã¼ãã®nameã¨emailããã¼ãã«ã§è¡¨ç¤ºããããããã®ã¬ã³ã¼ãã®æä½ã§ããShowï¼è¡¨ç¤ºï¼ãEditï¼ç·¨éï¼ãDestroyï¼åé¤ï¼ãå®ç¾©ããã¦ãããã¨ãåããã¾ãã
ãlink_toã¯Railsãæä¾ãããã«ãã¼ã¡ã½ããã®1ã¤ã§ããæåéããªã³ã¯ï¼ã¢ã³ã«ã¼ããã¹ãï¼ã®htmlãåºåãã¾ãã第1å¼æ°ã«ãªã³ã¯ã®è¡¨ç¤ºæååã第2å¼æ°ã«ãã¹ãæå®ãã¾ãã第3å¼æ°ä»¥éã¯ããã·ã¥ã§ãªãã·ã§ã³ãæå®ãããã¨ãã§ãã¾ãã
ããªããShowã¨Destroyã®ç¬¬2å¼æ°ã¯å°ãç¹æ®ãªçç¥å½¢ã¨ãªã£ã¦ãããuserã¬ã³ã¼ããã®ãã®ãæå®ããã¦ãã¾ããããã«å¯¾å¿ãããã¹ã¯/users/:idã§ãï¼userãªãã¸ã§ã¯ãã®idå¤ã1ã®å ´åã¯ï¼/users/1ã®ãããªãã¹ãçæããã¾ãã
ãEditã®ç¬¬2å¼æ°ã«ããedit_user_pathã¯Railsãæä¾ããURLãã«ãã¼ã§ããããã«ãã¢ãã«ãªãã¸ã§ã¯ãã渡ããã¨ã§ã対å¿ãããã¹ãçæã§ãã¾ããedit_user_pathã¯/users/:id/editã表ãã¾ãã®ã§ãï¼userãªãã¸ã§ã¯ãã®idå¤ã1ã®å ´åã¯ï¼/users/1/editã®ãããªãã¹ãçæããã¾ãã
Userã¢ãã«ã¤ã³ã¹ã¿ã³ã¹çæï¼newã¢ã¯ã·ã§ã³ï¼
ãUsersãªã½ã¼ã¹ä¸è¦§ããå®éã«usersãã¼ãã«ã¸ã¬ã³ã¼ããä½æãã¦ã¿ã¾ããããhttp://localhost:3000/usersã®ç»é¢ããNew Userã®ãªã³ã¯ãã¯ãªãã¯ããã¨ä»¥ä¸ã®ç»é¢ã¸é·ç§»ãã¾ãã
ãnameã¨emailã®å ¥åç»é¢ãåºåããã¾ãããnameãemailãé©å½ã«å ¥åãã¦Create Userãã¿ã³ãã¯ãªãã¯ããã¨usersãã¼ãã«ã®ã¬ã³ã¼ããå ¥åãã¦çæãããã¨ãã§ãã¾ããã¬ã³ã¼ãä½æåã«è©²å½ããã³ã³ããã¼ã©ã¼ã¨ãã¥ã¼ãã¡ã¤ã«ã確èªãã¦ã¿ã¾ãããã
class UsersController < ApplicationController â¦ï¼ä¸ç¥ï¼â¦ def new @user = User.new end â¦ï¼ä¸ç¥ï¼â¦
<h1>New User</h1> <%= render 'form', user: @user %> <%= link_to 'Back', users_path %>
ãã³ã³ããã¼ã©ã¼ã§ã¯Userã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãnewãã¦@userã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ä»£å ¥ãã¦ãã¾ãããã¥ã¼ã§ã¯formã¨ããé¨åãã³ãã¬ã¼ãããrenderã¡ã½ãããç¨ãã¦ã¬ã³ããªã³ã°ãã¦ãã¾ããrenderã¡ã½ããã¯Railsãæä¾ãããã«ãã¼ã¡ã½ããã§ããã¥ã¼ãã¡ã¤ã«ãç¹å®ã®ãããã¯ãã¨ã«åãåºãç¨éã¨ãã¦ä½¿ãã¾ããåãåºããããã³ãã¬ã¼ãã®ãã¨ãé¨åãã³ãã¬ã¼ãã¨å¼ã³ã¾ãã
ã第1å¼æ°ãé¨åãã³ãã¬ã¼ãã®ååã§ãã第2å¼æ°ã§ã¯ãuserã¨ãããã¼ã«ã«å¤æ°ã«@userã¤ã³ã¹ã¿ã³ã¹å¤æ°ãä»£å ¥ãã¦ãã¾ãããããããã¨ã§ãformé¨åãã³ãã¬ã¼ãã§userã¨ããå¤æ°ã使ç¨ãããã¨ãã§ãã¾ãã
ãããã§ã¯formé¨åãã³ãã¬ã¼ãã確èªãã¦ã¿ã¾ããããé¨åãã³ãã¬ã¼ãã¯ãªã½ã¼ã¹ã¨åä¸ã®ãã¥ã¼ãã£ã¬ã¯ããªã«ãã¢ã³ãã¼ã¹ã³ã¢ãä»ä¸ããããã¡ã¤ã«åã®ãã®ãå¼ã³åºããã¾ãããã®ã±ã¼ã¹ã§ã¯app/views/users/_form.html.erbãé¨åãã³ãã¬ã¼ãã¨ãªãã¾ãã
<%= form_for(user) do |f| %> â¦ï¼ä¸ç¥ï¼â¦ <div class="field"> <%= f.label :name %> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %> <%= f.text_field :email %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
ãform_forãã«ãã¼ã¡ã½ããã§Userã¢ãã«ã®ã¬ã³ã¼ããçæããããã«userå¤æ°ã使ç¨ãããã©ã¼ã é¢é£ã®htmlãä½æãã¦ãã¾ããform_forã¡ã½ããã®å¼æ°ã«ActiveRecordã®ã¤ã³ã¹ã¿ã³ã¹ãæå®ãã¦å¤æ°fããããã¯ã«æ¸¡ãã¦ãã¾ãããã®ããã«å®ç¾©ããã¨ããããã¯å ã§ãf.label :nameãã¨ãããã¨ã§ã©ãã«ã®htmlãããf.text_field :emailãã¨ãããã¨ã§ããã¹ããã£ã¼ã«ãã®htmlãçæã§ãã¾ãã
ãå ·ä½çã«ã¯ã<label for="user_name">Name</label>ãã¨ããhtmlã¨ã<input type="text" name="user[name]" id="user_name">ãã¨ããhtmlãçæããã¾ããããã¹ããã£ã¼ã«ãã®nameå±æ§ã®å¤ãuser[name]ãidå±æ§ã®å¤ãuser_nameã¨ãªã£ã¦ãããã¨ã«æ³¨ç®ãã¦ãã ããããªã½ã¼ã¹å+ãªã½ã¼ã¹ã®ã«ã©ã åã®ã«ã¼ã«ã§idå¤ãçæããããã¨ãåããã¾ãã
ãf.submitã¡ã½ããã¯Create Userãã¿ã³ã®htmlãçæãã¦ãã¾ãã