ruby-on-rails - ruby rails : Set fields in controller

标签 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

最佳答案

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

@player.location = 'UK'

关于ruby-on-rails - ruby rails : Set fields in controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404064/

相关文章:

ruby-on-rails - Rails3 has_and_belongs_to_many 定制

ruby-on-rails - rails 5.1。 : cannot get JSON object from string

ruby - 当 sqlite3 不在/usr/local 时,如何在 linux 上安装 sqlite-ruby?

ruby-on-rails - 使用 linux 脚本使连续的 rake 任务运行(启动、停止等)

ruby-on-rails - 目录 : Securing a Closed API

mysql - Ruby on Rails 中的自定义搜索

ruby-on-rails - 单表继承与 CRUD 和 Rails 中的表单

css - Rails 使用 sass 导入所有 css

ruby-on-rails - 为什么添加这样的 Rails 中间件会导致无休止的重定向?

ruby-on-rails - 在 ruby​​ 中递归搜索哈希并返回一个项目数组