Techioz Blog

Ruby on Rails 7 - 更新アクションの実行後の Rails ビュー エラー

概要

Ruby on Rails 7 - 更新アクションの実行後の Rails ビュー エラー

ツール モデルがあります。以下はその属性です。

class CreateTools < ActiveRecord::Migration[7.0]
  def change
    create_table :tools do |t|
      t.string :product_name
      t.string :graph_name
      t.string :process_capability_graph
      t.string :process_performance_graph

      t.timestamps
    end
  end
end

以下はビューコードです:

ご覧のとおり、ツール モデルで使用可能な値を出力しようとしています。

<table class="table">
        <tbody>
        <tr>
        <th scope="row">Current Name</th>
        <% if @software_settings.product_name.nil? %>
        <td>No data</td>
        <% else %>
        <td> <%= @software_settings.product_name %></td>
        <% end %>
        </tr>

        <tr>
        <th scope="row">Graph Heading </th>
        <% if @software_settings.graph_name.nil? %>
        <td>No data</td>
        <% else %>
        <td> <%= @software_settings.graph_name %></td>
        <% end %> 
        </tr>

        <tr>
        <th scope="row">Process Capability Graph </th>
        <% if @software_settings.process_capability_graph.nil? %>
        <td>No data</td>
        <% else %>
        <td> <%= @software_settings.process_capability_graph.capitalize %></td>
        <% end %> 
        </tr>

        <tr>
        <th scope="row">Process Performance Graph </th>
        <% if @software_settings.process_performance_graph.nil? %>
        <td>No data</td>
        <% else %>
        <td> <%= @software_settings.process_performance_graph.capitalize  %></td>
        <% end %> 
        </tr>
        </li>
        </tbody>
        </table>

以下はコントローラーのコードです。

#Method defined to display current Tool data. 
  def tools
    @software_settings = Tool.first
  end #End of tools method. 


  #Method defined to create a new Tool. 
  def update_tool_record

    update_attributes = {}
    # Check if new values are present for each attribute and add them to the update_attributes hash if they are
    update_attributes[:product_name] = params[:product_name] if params[:product_name].present?
    update_attributes[:graph_name] = params[:graph_name] if params[:graph_name].present?
    update_attributes[:process_capability_graph] = params[:process_capability_graph] if params[:process_capability_graph].present?
    update_attributes[:process_performance_graph] = params[:process_performance_graph] if params[:process_performance_graph].present?
    # Update the record with the provided attributes
    Tool.first.update!(update_attributes)
    render 'tools'
  end #End of new_tool_record method. 

値を更新すると、このエラーが表示されます。

undefined method `product_name' for nil:NilClass

ただし、ブラウザを更新すると、このエラーは表示されません。アップデート後、一度だけ表示されます。

理解できません。 Rails コンソールで @software_settings インスタンス変数の値を確認したところ、次の値が含まれています。

irb(main):001:0> Tool.first
  Tool Load (1.2ms)  SELECT "tools".* FROM "tools" ORDER BY "tools"."id" ASC LIMIT $1  [["LIMIT", 1]]
=>
#<Tool:0x00000217ee9ba278
 id: 1,
 product_name: "tejinder kalsi",
 graph_name: "new test",
 process_capability_graph: "line",
 process_performance_graph: "bar",
 created_at: Wed, 23 Aug 2023 12:05:56.039374000 UTC +00:00,
 updated_at: Tue, 12 Sep 2023 12:19:24.383130000 UTC +00:00>
irb(main):002:0> exit

解決策

更新フローで @software_settings を設定していないため、ビューでは nil になります。おそらく、Tool.first.update!(update_attributes) の代わりに tools.update!(update_attributes) を実行するつもりでした。