新的强参数未保存在数据库中

New strong parameter not being saved in database

我使用迁移在 Rails 中的 table 添加了一个名为 "version" 的新列,并手动将相应的参数添加到相应控制器中允许的强参数中:

def endpoint_params
params.require(:endpoint).permit(:hostname, :username, :password, :connection_string, :entity_id, :created_at,
                                 :updated_at, :endpoint_type_id, :endpoint_app_id, :environment_id, :created_by,
                                 :updated_by, :version)
end

但是,当我尝试在更新操作中设置新参数并将其保存到数据库时,它根本没有被保存。我使用的代码是:

def update

begin

  @environment = Environment.find(params[:env])

  version_no = (EndpointsFile.where("environment_id = ?", @environment.id).maximum('version') || 0) + 1

  @endpoint.updated_by = current_user.id
  @endpoint.version = version_no

  respond_to do |format|

    if @endpoint.update(endpoint_params)

      @endpoints = Endpoint.where("environment_id = ? and id <> ?", @environment.id, @endpoint.id)

      EndpointsFile.create!(version: version_no, date: Time.now, environment_id: @environment.id)

      @endpoints.each do |e|

        e.version = version_no
        e.save

        puts "e.version: #{e.version}"   **=> HERE IT PRINTS e.version: and the field is null in the database**

      end

      format.html { redirect_to env_endpoints_path(env: @environment.id), notice: t('.notice') }
      format.json { render :show, status: :ok, location: @endpoint }
    else
      format.html { render :edit }
      format.json { render json: @endpoint.errors, status: :unprocessable_entity }
    end
  end

  rescue => exception

    logger.error { "endpoints_controller.update -> Ocorreu um erro ao atualizar o endpoint: #{exception.message}" }

    respond_to do |format|
       format.html { redirect_to env_endpoints_path(env: @environment.id), alert: "Ocorreu um erro ao atualizar o endpoint: #{exception.message}" and return }
       format.json { render json: @endpoint.errors, status: :unprocessable_entity }
    end

  end
end

这是端点模型:

class Endpoint < ApplicationRecord

 belongs_to :entity
 belongs_to :environment
 belongs_to :endpoint_app
 belongs_to :endpoint_type

 has_many :configs_histories

 has_paper_trail

end

数据库中的table是:

create_table "endpoints", force: :cascade do |t|
   t.string   "hostname"
   t.string   "username"
   t.string   "password"
   t.string   "connection_string"
   t.integer  "entity_id"
   t.datetime "created_at",        null: false
   t.datetime "updated_at",        null: false
   t.integer  "endpoint_type_id"
   t.integer  "endpoint_app_id"
   t.integer  "environment_id"
   t.integer  "created_by"
   t.integer  "updated_by"
   t.integer  "version"
   t.index ["endpoint_app_id"], name: "index_endpoints_on_endpoint_app_id"
   t.index ["endpoint_type_id"], name: "index_endpoints_on_endpoint_type_id"
   t.index ["entity_id"], name: "index_endpoints_on_entity_id"
   t.index ["environment_id"], name: "index_endpoints_on_environment_id"
 end

我做错了什么?为什么命令 e.save 不起作用?

终于找到解决办法了。

version是保留关键字,不能作为列名使用。

检查 link: http://reservedwords.herokuapp.com/words?page=9

因为你是在update里面计算version_no,所以你不需要在controller中传递它或者在strong参数中允许,只需执行以下操作

@endpoint.update(endpoint_params.merge(version: version_no))