Ruby on Railsによる野球選手マスタメンテナンスアプリケーションの作成 その2
コントローラの修正
次に、コントローラを修正します。
class MstBaseballPlayersController < ApplicationController def index list render :action => 'list' end # GETs should be safe # (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def list # 検索条件 conditions = nil conditions = ["player_name like ?"] if player_name = params[:player_name] player_name = player_name + "%" conditions << player_name else conditions << "%" end @mst_baseball_player_pages, @mst_baseball_players = paginate :mst_baseball_players, :per_page => 10, :conditions=>conditions end def show @mst_baseball_player = MstBaseballPlayer.find(params[:id]) end def new @mst_baseball_player = MstBaseballPlayer.new end def create @mst_baseball = MstBaseballPlayer.create params[:mst_baseball_player] @mst_baseball.main_image_path = @mst_baseball.main_url @mst_baseball.detail_image_path = @mst_baseball.detail_url @mst_baseball.expanded_image_path = @mst_baseball.expanded_url if @mst_baseball.update flash[:notice] = 'MstBaseballPlayer was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end def edit @mst_baseball_player = MstBaseballPlayer.find(params[:id]) end def update @mst_baseball = MstBaseballPlayer.find(params[:mst_baseball_player][:id]) unless @mst_baseball.update_attributes(params[:mst_baseball_player]) render :action => 'edit' end if params[:mst_baseball_player][:file_data] && params[:mst_baseball_player][:file_data].length > 0 @mst_baseball.main_image_path = @mst_baseball.main_url @mst_baseball.detail_image_path = @mst_baseball.detail_url @mst_baseball.expanded_image_path = @mst_baseball.expanded_url end if @mst_baseball.update flash[:notice] = 'MstBaseballPlayer was successfully updated.' redirect_to :action => 'show', :id => @mst_baseball else render :action => 'edit' end end def destroy MstBaseballPlayer.find(params[:id]).destroy redirect_to :action => 'list' end end
ビューの修正 1/2
最後に、ビューを修正します。
<div class="title" style="margin-bottom: 10px;"> <h2>野球選手マスタの新規作成</h2> </div> <% form_for :mst_baseball_player, @mst_baseball_player, :url=>{:action=>'create'}, :html=>{:multipart=>true} do |f| %> <%= render(:partial=>'new', :locals=>{:f=>f}) %> <%= submit_tag "新規作成" %> <% end %> <%= link_to '戻る', :action => 'list' %>
<%= error_messages_for 'mst_baseball_player' %> <table class="confirm"> <tr> <th>選手名</th> <td><%= f.text_field 'player_name' -%></td> </tr> <tr> <th>背番号</th> <td><%= f.text_field 'uniform_number' -%></td> </tr> <tr> <th>プロフィール</th> <td> <%= f.text_area 'profile', :cols=>50, :rows=>5 %> </td> </tr> <tr> <th>記録</th> <td> <%= f.text_area 'record', :cols=>50, :rows=>5 %> </td> </tr> <tr> <th>コメント</th> <td> <%= f.text_area 'comment', :cols=>50, :rows=>5 %> </td> </tr> <tr> <th>投手フラグ</th> <td> <%= f.check_box 'pitcher_flag' %> </td> </tr> <tr> <th>勝数</th> <td><%= f.text_field 'win_count' -%></td> </tr> <tr> <th>負数</th> <td><%= f.text_field 'defeat_count' -%></td> </tr> <tr> <th>セーブ数</th> <td><%= f.text_field 'save_count' -%></td> </tr> <tr> <th>防御率</th> <td><%= f.text_field 'earned_run_average' -%></td> </tr> <tr> <th>出場試合数</th> <td><%= f.text_field 'games_started' -%></td> </tr> <tr> <th>打率</th> <td><%= f.text_field 'batting_average' -%></td> </tr> <tr> <th>HR</th> <td><%= f.text_field 'home_run' -%></td> </tr> <tr> <th>打点</th> <td><%= f.text_field 'ribby' -%></td> </tr> <tr> <th>盗塁</th> <td><%= f.text_field 'base_steal' -%></td> </tr> <tr> <th><label for='image_file_data'>画像ファイル:</label></th> <td colspan="5"><%= f.file_field :file_data %></td> </tr> <tr> <th>メイン画像ファイルパス</th> <td><%= f.text_field 'main_image_path' -%></td> </tr> <tr> <th>詳細画像ファイルパス</th> <td><%= f.text_field 'detail_image_path' -%></td> </tr> <tr> <th>拡大画像ファイルパス</th> <td><%= f.text_field 'expanded_image_path' -%></td> </tr> </table>