我有以下代码来创建一个新的播放器。 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
最佳答案
只需在 @player = Player.new(params[:player])
行之后添加此行:
@player.location = 'UK'
关于ruby-on-rails - ruby rails : Set fields in controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404064/