我是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
答案 0 :(得分:2)
检查ActiveRecord Callbacks。查找before_save
和after_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
答案 1 :(得分:2)
有很多方法可以做到这一点。但是你不能在self.save
或before_save
中使用after_save
,因为save会再次调用这些before_save和after_save,因此堆栈级别太深。使用update_column
代替它不会触发任何回调
after_save :add_point1_and_point2
def add_point1_and_point2
update_column(:point3, point1 + point2)
end
答案 2 :(得分:2)
第一件事:您错过了结果列可能是列'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
因为我们只是分配属性(sum为point1和point2的+),所以这不会保存它。这只是分配,因为我们正在触发这个before_save。 因此,当触发保存时,将自动保存此总和值。 :)