使用 Ruby 添加数字并保存到数据库 Rails

Adding numbers and save to the database using Ruby On Rails

我是 RoR 的新手,我很难尝试一些语法。

我不知道如何添加两个从这里输入的数字:

<div class="field">
<%= f.label :point1 %><br>
<%= f.number_field :point1 %>

</div>
<div class="field">
<%= f.label :point2 %><br>
<%= f.number_field :point2 %>

</div>

我还想将数字保存到数据库中,包括它们的总和。 我已经从网上搜索过了,但我对结果不满意。

希望有人能帮助我。谢谢。

顺便说一句,我知道如何将输入的两个数字保存在数据库中,除了总和。

这些是我的代码!

player.rb

 class Player < ActiveRecord::Base
    before_save :add_point1_and_point2
private
def add_point1_and_point2
  self.point3 = self.point1 + self.point2
  self.save
end
end

_form.html.erb

<%= form_for(@player) do |f| %>
  <% if @player.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@player.errors.count, "error") %> prohibited this player from being saved:</h2>

      <ul>
      <% @player.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<% sum = 0 %>
  <div class="field">
    <%= f.label :point1 %><br>
    <%= f.number_field :point1 %>

  </div>
  <div class="field">
    <%= f.label :point2 %><br>
    <%= f.number_field :point2 %>

  </div>


  </div>
  <div class="actions">
    <%= f.submit %>

  </div>
<% end %>

players_controller.rb

class PlayersController < 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

勾选ActiveRecord Callbacks。寻找 before_saveafter_save

在您的 model 中,您应该有类似于此的代码

before_save :add_point1_and_point2
private
def add_point1_and_point2
  sum_value = self.point1 + self.point2
  self.assign_attributes(sum_column: sum_value)
end

好吧,有几种方法可以做到这一点。但是你不能在这里使用 before_saveafter_save 中的 self.save,因为保存将再次调用这些 before_save 和 after_save,因此堆栈级别太深。使用 update_column 而不是它不会触发任何回调

after_save :add_point1_and_point2
def add_point1_and_point2
  update_column(:point3, point1 + point2)
end

第一件事:您缺少结果列可能是列 'sum'

#这将为玩家添加一个新的列总和。

rails g migration add_sum_to_players sum:integer 
rake db:migrate

接下来在您的模型文件中试试这个:

 #player.rb
class Player < ActiveRecord::Base 

before_save :calculate_sum

private 
  def calculate_sum
    result = point1 + point2 
    self.assign_attributes(sum: result)
  end
end

因为我们只是分配属性(和点1和点2的+),所以这不会保存它。这只是分配,因为我们正在触发此 before_save。 因此,当触发保存时,此总和值将自动保存。 :)