Top > Ruby on Railsあれこれ > 従業員登録機能の追加

  0
  1
  2
<% form_tag :action => 'edit_for_add' do %>
  <%= submit_tag "追加" %>
<% end %>
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
<html>
<head>
  <title>Tutorial: Employee List</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <%= stylesheet_link_tag 'ror' %>
</head>
<body>
<h2>Tutorial: Employee List</h2>
 
<table border="1">
  <tr style="background-color:pink">
    <th>Id</th><th>Name</th><th>JobType</th><th>Salary</th><th>Department</th><th>編集</th>
  </tr>
  <% for e in @employees %>
  <tr style="background-color:<%= cycle('white', 'yellow') %>">
    <td align="right"><%=h e.id %></td>
    <td><%=h e.name %></td>
    <td><%=h e.job_type %></td>
    <td align="right"><%= number_with_delimiter(e.salary, delimiter=",") %></td>
    <td><%=h r_label(e.department_id, @departments, "id", "name") %></td>
    <td><%= link_to "編集", {:action => 'edit', :id => e.id} %></td>
  </tr>
  <% end %>
</table>
  
<% form_tag :action => 'edit_for_add' do %>
  <%= submit_tag "追加" %>
<% end %>
  
</body>
</html>

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
<html>
<head>
  <title>Tutorial: Employee Edit</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <%= stylesheet_link_tag 'ror' %>
</head>
<body>
 
<h2>Tutorial: Employee Edit For Add</h2>
 
<% form_for(@employee, :url => { :action => 'confirm_for_add', :id => @employee.id }) do |f| %>
  <%= f.error_messages %>
  <table>
    <tr>
      <td><%= f.label :name %></td>
      <td><%= f.text_field :name %></td>
    </tr>
    <tr>
      <td>JobType</td>
      <td><%= f.select :job_type, ['CLERK', 'SALESMAN', 'MANAGER', 'ANALYST', 'PRESIDENT'] %></td>
    </tr>
    <tr>
      <td><%= f.label :salary %></td>
      <td><%= f.text_field :salary %></td>
    </tr>
    <tr>
      <td>Department</td>
      <td><%= f.select :department_id, Department.find(:all).collect {|p| [ p.name, p.id ] } %></td>
    </tr>
  </table>
  <%= f.submit "確認" %>
  <%= f.submit "戻る", {:type => "button", :onClick => "this.form.action='/employees/index'; this.form.submit();"} %>
<% end %>
</body>
</html>

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
<html>
<head>
  <title>Tutorial: Employee Confirm</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <%= stylesheet_link_tag 'ror' %>
</head>
<body>
 
<h2>Tutorial: Employee Confirm For Add</h2>
 
<% form_for(@employee, :url => { :action => 'add' } ) do |f| %>
  <%= f.error_messages %>
  <%= f.hidden_field :name %>
  <%= f.hidden_field :job_type %>
  <%= f.hidden_field :salary %>
  <%= f.hidden_field :department_id %>
  <%= f.hidden_field :name %>
  <table>
    <tr>
      <td>Name</td><td><%=h @employee.name %></td>
    </tr>
    <tr>
      <td>JobType</td><td><%=h @employee.job_type %></td>
    </tr>
    <tr>
      <td>Salary</td>
      <td><%= number_with_delimiter(@employee.salary, delimiter=",") %></td>
    </tr>
    <tr>
      <td>Department</td><td><%=h @department_name %></td>
    </tr>
  </table>
  <%= f.submit "登録" %>
  <%= f.submit "戻る", {:type => "button", :onClick => "this.form.action='/employees/back_to_edit_for_add'; this.form.submit();"}%>
<% end %>
</body>
</html>
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
  #追加編集画面表示
  def edit_for_add
    @employee = Employee.new
    render :action => 'edit_for_add.html.erb'
  end
  
  #追加編集内容確認画面表示
  def confirm_for_add
    @department_name = Department.find(params[:employee][:department_id]).name
    @employee = Employee.new(params[:employee])
    
    #バリデーションチェック
    if @employee.valid?
      render :action => 'confirm_for_add.html.erb'
    else
      #エラー
      render :action => 'edit_for_add.html.erb'
    end
  end
  
  #追加編集内容確認画面→追加編集画面に戻る
  def back_to_edit_for_add
    @employee = Employee.new(params[:employee])
    render :action => 'edit_for_add.html.erb'
  end
  
  #追加処理
  def add
    @employee = Employee.new(params[:employee])
    if @employee.save
      flash[:notice] = 'Employee was successfully updated.'
      index
    else
      #エラー
      confirm_for_add
    end
  end

添付ファイル: filedb_tuto_add_5.jpg 187件 [詳細] filedb_tuto_add_4.jpg 199件 [詳細] filedb_tuto_add_3.jpg 204件 [詳細] filedb_tuto_add_2.jpg 211件 [詳細] filedb_tuto_add_1.jpg 209件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:25:44 (2923d)