ãªã¬ãªã¬scaffold generator ãä½ã
Ruby on Rails ã® Scaffold generator ã¯ã¢ããªã®å ã«ãªãã³ã¼ããçæããã«ä¾¿å©ã§ããå®éã®ã¢ããªãä½ãéã«ã¯ãXMLãã¬ã¹ãã³ã¹ããã³ã¼ãã¯è¦ããªãã¨ããæ¥æ¬èªãããªãã¨ãããã³ãã¬ã¼ããã¤ãã¤ãã ã¨ãããããä¸æºç¹ãããã¨æãã¾ãã æ¨æºã®Scaffoldã®ä½æããã³ã¼ããå°ãå¤ããã ããªãã¨ã¦ãç°¡åã§ãã
ãªã¬ãªã¬scaffold generatorãç½®ãå ´æ
ã¾ãããªã¬ãªã¬scaffold generator ãç½®ãå ´æã§ãããRuby on Rails guidesの 2.3 The Rails Generation: Generatorsã«ããããã« RAILS_ROOT/lib/generators ã ~/.rails/generators ãªã©ãèãããã¾ããããã¸ã§ã¯ãåºæã®ãªã¬ãªã¬scaffoldãªã RAILS_ROOT/lib/generatorsãè¯ãããããã¾ããããä»å㯠ãªã¬ãªã¬ ã¨ãããã¨ã§ ~/.rails/generators ã«ä½ã£ã¦ã¿ã¾ãã
æ¨æºã®Scaffoldãã³ãã¼
Scaffold generatorã¯generatorã®ã³ã¼ãã¨çæããã³ã¼ãã®ãã³ãã¬ã¼ãããåºæ¥ã¦ãã¾ããä»åã¯generatorã¯Scaffoldããã®ã¾ã¾ä½¿ãã®ã§ã ScaffoldGeneratorã¯ã©ã¹ãç¶æ¿ããã ãã§ãããã³ãã¬ã¼ã㯠Scaffoldã®ãã®ãã³ãã¼ãèªåç¨ã«å¤æ´ãã¾ãã
% mkdir ~/.rails/generators/my_scaffold % cd ~/.rails/generators/my_scaffold % cp -r /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails_generator/generators/components/scaffold/templates . # â gem ã®ãã¹ã¯èªåã®ç°å¢ã«åããã¦å¤ãã¦ä¸ãã % cat > my_scaffold_generator.rb class MyScaffoldGenerator < ScaffoldGenerator end ^D % % vi templates/controller.rb % vi templates/view_index.html.erb .... %
使ã£ã¦ã¿ã
% rails test % cd test % ./script/generate --help Usage: script/generate generator [options] [args] Rails Info: -v, --version Show the Rails version number and quit. ... Installed Generators User: my_scaffold â ãªã¬ãªã¬scaffold generatorãããã¾ã !! Rubygems: gruff, i18n, i18n_locale, i18n_scaffold, i18n_translation, rspec, rspec_controller, rspec_model, rspec_scaffold, web_service Builtin: controller, helper, integration_test, mailer, metal, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration .... % ./script/generate my_scaffold todo due:date task:string â 使ã£ã¦è©¦ãã¦ã¿ã
ã¨ã¦ãç°¡åã§ã (^_^)
ç§ã®ä½ã£ã ãªã¬ãªã¬ãã³ãã¬ã¼ã
- controller.rb
# <%= class_name %>ã³ã³ããã©ã¼ class <%= controller_class_name %>Controller < ApplicationController # ä¸è¦§ def index @<%= table_name %> = <%= class_name %>.all end # 詳細表示 def show @<%= file_name %> = <%= class_name %>.find(params[:id]) end # æ°è¦ä½æç»é¢ def new @<%= file_name %> = <%= class_name %>.new end # ç·¨éç»é¢è¡¨ç¤º def edit @<%= file_name %> = <%= class_name %>.find(params[:id]) end # æ°è¦ä½æå¦ç def create @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>]) if @<%= file_name %>.save flash[:notice] = '<%= class_name %> ãä½æãã¾ããã' redirect_to(@<%= file_name %>) else render :action => "new" end end # ç·¨éå¦ç def update @<%= file_name %> = <%= class_name %>.find(params[:id]) if @<%= file_name %>.update_attributes(params[:<%= file_name %>]) flash[:notice] = '<%= class_name %> ãæ´æ°ãã¾ããã' redirect_to(@<%= file_name %>) else render :action => "edit" end end # åé¤ def destroy @<%= file_name %> = <%= class_name %>.find(params[:id]) @<%= file_name %>.destroy redirect_to(<%= table_name %>_url) end end
- view_index.html.erb
<h1><%= plural_name %>ä¸è¦§</h1> <table> <tr> <% for attribute in attributes -%> <th></th> <th><%= attribute.column.human_name %></th> <% end -%> </tr> <%% @<%= plural_name %>.each do |<%= singular_name %>| %> <tr> <td> <%%= link_to '表示', <%= singular_name %> %> <%%= link_to 'ç·¨é', edit_<%= singular_name %>_path(<%= singular_name %>) %> <%%= link_to 'åé¤', <%= singular_name %>, :confirm => 'åé¤ãã¦ããã§ãã?', :method => :delete %> </td> <% for attribute in attributes -%> <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td> <% end -%> </tr> <%% end %> </table> <br /> <%%= link_to 'æ°è¦ä½æ', new_<%= singular_name %>_path %>
- view_show.html.erb
<h1><%= singular_name %> 詳細</h1> <div class="detail"> <dl> <% for attribute in attributes -%> <dt><%= attribute.column.human_name %>:</dt> <dd><%%=h @<%= singular_name %>.<%= attribute.name %> %></dd> <% end -%> </dl> </div> <%%= link_to 'æ»ã', <%= plural_name %>_path %>
- view_edit.html.erb (view_new.html.erbãã»ã¼åã)
<h1><%= singular_name %> ç·¨é</h1> <div class="entry"> <%% form_for(@<%= singular_name %>) do |f| %> <%%= f.error_messages %> <dl> <% for attribute in attributes -%> <dt><%%= f.label :<%= attribute.name %> %></dt> <dd><%%= f.<%= attribute.field_type %> :<%= attribute.name %> %></dd> <% end -%> </dl> <div class="op_buttons"> <%%= f.submit 'æ´æ°' %> </div> <%% end %> </div> <%%= link_to 'æ»ã', <%= plural_name %>_path %>
- style.css ã®è¿½å å
div.detail, div.entry { } div.detail dt, div.entry dt { float: left; width: 5em; padding: 2px 0px 2px 10px; } div.detail dd, div.entry dd { margin-left: 5em; padding: 2px 0px 2px 20px; }