表单不使用Select Tag Ruby更新数据库

时间:2016-03-15 12:25:12

标签: ruby-on-rails ruby

我最近在数据库中添加了一个名为“sport”的新列。我试图使用表单中的select_tag更新列中的值,但每次我尝试数据都没有更新。表格的其余部分目前正在正常工作和更新。

select视频目前在我的视图中就像这样实现。

<%= 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] ]))%>

:sport在表格中声明为整数。

控制器目前看起来像这样:

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

1 个答案:

答案 0 :(得分: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] ])%>