title: Adding numbers and save to the database using Ruby On Rails
body: <p>Im new to RoR and I have a hard time in experimenting some syntax.</p>
  
  <p>I don't know how to add two inputted numbers coming from here:</p>
  
  <pre><code>&lt;div class="field"&gt;
  &lt;%= f.label :point1 %&gt;&lt;br&gt;
  &lt;%= f.number_field :point1 %&gt;
  
  &lt;/div&gt;
  &lt;div class="field"&gt;
  &lt;%= f.label :point2 %&gt;&lt;br&gt;
  &lt;%= f.number_field :point2 %&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>I also want to save the numbers to database including their sum.
  I already searched it from the Internet but I'm not satisfied with the results.</p>
  
  <p>I hope someone will help me. Thank you.</p>
  
  <p>by the way, I know how to save the two inputted numbers in database except the sum.</p>
  
  <p>these are my codesss!</p>
  
  <p>player.rb</p>
  
  <pre><code> class Player &lt; ActiveRecord::Base
      before_save :add_point1_and_point2
  private
  def add_point1_and_point2
    self.point3 = self.point1 + self.point2
    self.save
  end
  end
  </code></pre>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@player) do |f| %&gt;
    &lt;% if @player.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@player.errors.count, "error") %&gt; prohibited this player from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @player.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
  &lt;% sum = 0 %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :point1 %&gt;&lt;br&gt;
      &lt;%= f.number_field :point1 %&gt;
  
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :point2 %&gt;&lt;br&gt;
      &lt;%= f.number_field :point2 %&gt;
  
    &lt;/div&gt;
  
  
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
  
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>players_controller.rb</p>
  
  <pre><code>class PlayersController &lt; ApplicationController
  
    before_action :set_player, only: [:show, :edit, :update, :destroy]
  
    # GET /players
    # GET /players.json
    def index
      @players = Player.all
    end
  
    # GET /players/1
    # GET /players/1.json
    def show
    end
  
    # GET /players/new
    def new
      @player = Player.new
      end
  
  
  
  
    # GET /players/1/edit
    def edit
    end
  
    # POST /players
    # POST /players.json
    def create
  
     # @player = Player.new(sum_num :point3)
  
      @player = Player.new(player_params)
  
      respond_to do |format|
        if @player.save
  
          format.html { redirect_to @player, notice: 'Player was successfully created.' }
          format.json { render :show, status: :created, location: @player }
        else
          format.html { render :new }
          format.json { render json: @player.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /players/1
    # PATCH/PUT /players/1.json
    def update
      respond_to do |format|
        if @player.update(player_params)
          format.html { redirect_to @player, notice: 'Player was successfully updated.' }
          format.json { render :show, status: :ok, location: @player }
        else
          format.html { render :edit }
          format.json { render json: @player.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /players/1
    # DELETE /players/1.json
    def destroy
      @player.destroy
      respond_to do |format|
        format.html { redirect_to players_url, notice: 'Player was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
  
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_player
        @player = Player.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def player_params
        params.require(:player).permit(:point1, :point2)
      end
  
  end
  </code></pre>