2016-03-15 51 views
0

我最近在數據庫中添加了一個名爲「sport」的表中的新列。我試圖在表單中使用select_tag更新列中的值,但是每次嘗試數據都沒有更新。表單的其餘部分目前正在正常工作和更新。表單不使用選擇標記更新數據庫紅寶石

選擇標記目前在我的視圖中像這樣實現。

<%= f.label :sport %> 
<%= select_tag(:sport, options_for_select([['Basketball', 1], ['NRL', 2], ['Super Rugby', 3], ['AFL', 4], ['eSports', 5], ['Ice Hockey', 6], ['Horse Racing', 7], ['Tennis', 8], ['NFL', 9] ]))%> 

:運動被聲明爲在表中一個整數。

控制器目前看起來是這樣的:

class BetsController < ApplicationController 

    def new 
    @bet = Bet.new 
    end 

    def create 
    @bets = Bet.new(bet_params) 

    if @bets.save 
     flash[:success] = 'Bet Successfull Logged.' 
     redirect_to new_bet_path 
     else 
     flash[:danger] = 'Error, Bet has not been logged. Try again mate.' 
     redirect_to new_bet_path 
     end 
    end 

    def show 
    @bet = Bet.find(params[:id]) 
    end 

    def edit 
    @bet = Bet.find(params[:id]) 
    end 

    def update 
    @bet = Bet.find(params[:id]) 

     if @bet.update_attributes(bet_params) 
      flash[:success] = "Bet Updated!" 
      redirect_to bet_path(params[:id]) 
     else 
      render action: :edit 
     end 
    end 

    private 

    def bet_params 
     params.require(:bet).permit(:bet_placed, :game, :units_placed, :odds, :profit_or_loss, :date_of_bet, :resolved, :push, :sport, :bookmaker) 
    end 

end 
+0

當您提交表單時:哪些參數提交給控制器。新列是什麼類型? –

+0

新列的類型是整數。 整個表格是針對下注行的submmited。 –

回答

2

如果您檢查您的PARAMS我敢打賭哈希你會發現信息不存在。
嘗試使用select form_for幫手,而不是select_tag幫手。相反,您的代碼可能如下所示:

<%= f.select(:title, collection: [['Basketball', 1], ['NRL', 2], ['Super Rugby', 3], ['AFL', 4], ['eSports', 5], ['Ice Hockey', 6], ['Horse Racing', 7], ['Tennis', 8], ['NFL', 9] ])%>