2015-01-13 141 views
0

對於RoR而言,我很新,並且在試驗某些語法方面還很困難。使用Ruby On Rails添加數字並保存到數據庫

我不知道如何添加兩個輸入的號碼從這裏來的:

<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 

回答

2

檢查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 
+0

我得到這個錯誤: 堆棧層面太深 – ana

+0

ana,你應該在問題本身添加所有的代碼。你問這個問題,社區的答案! :) – gkolan

+0

ahhh:D我很愚蠢。哈哈! – ana

2

代碼那麼有幾種方式做到這一點。但是你不能在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 
+0

另一個錯誤出現: 的 「update_column(POINT3,點1 +點2)」 – ana

+0

的ActiveRecord :: ActiveRecordError在PlayersController#創建 – ana

+0

改變 'before_save' 到 'after_save的' 「不能一個新的記錄對象上更新」 再次 – Abk

2

1日的事情:你缺少你的結果列可能是列「和」

#這將在新列總和添加到播放器。

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。 因此,當保存將被觸發時,這個總和值將被自動保存。 :)

+0

請考慮接受這個如果這已經解決了你的問題。 你可以在這裏找到完整的代碼: https://gist.github.com/AjayROR/b051edaf31d4f827d71e – Ajay

相關問題