开发者

Ruby on Rails: Set fields in controller

开发者 https://www.devze.com 2023-03-13 20:06 出处:网络
I have the following code to create a new Player. Player.location is NOT NULL in the database but I do not want to show it in the new player form. How can I set the Player.location attribute (say they

I have the following code to create a new Player. Player.location is NOT NULL in the database but I do not want to show it in the new player form. How can I set the Player.location attribute (say they're all set to "UK" for now) in here?

# POST /player
# POST /player.xml
def create
  @player = Pl开发者_如何学运维ayer.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


Just add this line after your @player = Player.new(params[:player]) line:

@player.location = 'UK'
0

精彩评论

暂无评论...
验证码 换一张
取 消