Ruby on Rails:在控制器中设置字段

时间:2011-06-19 17:44:56

标签: ruby-on-rails ruby

我有以下代码来创建一个新的播放器。 Player.location在数据库中不是NULL,但我不想在新的播放器表单中显示它。如何在这里设置Player.location属性(现在它们都设置为“UK”)?

# POST /player
# POST /player.xml
def create
  @player = Player.new(params[:player])

  respond_to do |format|
    if @player.save
      flash[:notice] = 'Player was successfully created.'
      format.html { redirect_to(@player) }
      format.xml  { render :xml => @player, :status => :created, :location => @player }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @player.errors, :status => :unprocessable_entity }
    end
  end
end

1 个答案:

答案 0 :(得分:3)

只需在@player = Player.new(params[:player])行之后添加此行:

@player.location = 'UK'