сериализация столбца и сохранение

Я использую rails 4.2 с ruby 2.2.
Я пытаюсь сохранить некоторые данные внутри столбца в хэш-формате.

Я пытаюсь сохранить сведения о лицензии в licenseстолбце, который имеет тип textданных, и в соответствующей модели, которую я дал:

serialize :license, JSON

У меня это в контроллере:

params.require(:company_detail).permit(:company_name, :trading_name, :contact_name, :acn, :address, :suburb, :state, :post_code, :phone_number, :fax_number, :nrma_approved, :start_date, :release_date , :website_url ,:email, license: {date1: :date1, license1: :license1, date2: :date2, license2: :license2, date3: :date3, license3: :license3, date4: :date4, license4: :license4})

и по форме

= form_for @company_detail, remote: true, html: {class: 'form-horizontal form-label-left', data: {"parsley-validate" => ""}} do |f|
      = f.fields_for :license do |l|
        .row
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Date 1
              .col-md-8.col-xs-12
                = l.text_field :date1, value: @company_detail.license.present? ? @company_detail.license["date1"] : '', class: 'date-picker form-control has-feedback-left datePicker'
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Licence Number 1
              .col-md-8.col-xs-12
                = l.text_field :license1, value: @company_detail.license.present? ? @company_detail.license["license1"] : '', class: 'form-control'

Я попытался ввести несколько полей в форму и отправить, но, несмотря на то, что парамы поступают правильно, сохраненное значение равно null. Это журнал:

Parameters: {"utf8"=>"✓", "company_detail"=>{"license"=>{"date1"=>"121212", "license1"=>"12313", "date2"=>"122131123", "license2"=>"123123", "date3"=>"", "license3"=>"", "date4"=>"", "license4"=>""}}, "commit"=>"Save", "id"=>"1"}
CompanyDetail Load (0.3ms)  SELECT  "company_details".* FROM "company_details" WHERE "company_details"."id" = 

I'm using rails 4.2 with ruby 2.2. I'm trying to save some data inside a column in a hash format.

I'm trying to save license details in the license column which is of text datatype, and in the respective model i've given:

serialize :license, JSON

I have this in the controller:

params.require(:company_detail).permit(:company_name, :trading_name, :contact_name, :acn, :address, :suburb, :state, :post_code, :phone_number, :fax_number, :nrma_approved, :start_date, :release_date , :website_url ,:email, license: {date1: :date1, license1: :license1, date2: :date2, license2: :license2, date3: :date3, license3: :license3, date4: :date4, license4: :license4})

and in form

= form_for @company_detail, remote: true, html: {class: 'form-horizontal form-label-left', data: {"parsley-validate" => ""}} do |f|
      = f.fields_for :license do |l|
        .row
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Date 1
              .col-md-8.col-xs-12
                = l.text_field :date1, value: @company_detail.license.present? ? @company_detail.license["date1"] : '', class: 'date-picker form-control has-feedback-left datePicker'
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Licence Number 1
              .col-md-8.col-xs-12
                = l.text_field :license1, value: @company_detail.license.present? ? @company_detail.license["license1"] : '', class: 'form-control'

I tried entering a few fields in the form and submit, but even though the params are going in correctly, the value saved is null. This is the log:

Parameters: {"utf8"=>"✓", "company_detail"=>{"license"=>{"date1"=>"121212", "license1"=>"12313", "date2"=>"122131123", "license2"=>"123123", "date3"=>"", "license3"=>"", "date4"=>"", "license4"=>""}}, "commit"=>"Save", "id"=>"1"}
CompanyDetail Load (0.3ms)  SELECT  "company_details".* FROM "company_details" WHERE "company_details"."id" = $1 LIMIT 1  [["id", 1]]
 User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
 (1.0ms)  BEGIN
SQL (0.4ms)  UPDATE "company_details" SET "license" = $1, "updated_at" = $2 WHERE "company_details"."id" = $3  [["license", "{"date1":null,"license1":null,"date2":null,"license2":null,"date3":null,"license3":null,"date4":null,"license4":null}"], ["updated_at", "2016-05-01 12:15:44.918547"], ["id", 1]]

LIMIT 1 [["id", 1]]
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =

I'm using rails 4.2 with ruby 2.2.
I'm trying to save some data inside a column in a hash format.

I'm trying to save license details in the license column which is of text datatype, and in the respective model i've given:

serialize :license, JSON

I have this in the controller:

params.require(:company_detail).permit(:company_name, :trading_name, :contact_name, :acn, :address, :suburb, :state, :post_code, :phone_number, :fax_number, :nrma_approved, :start_date, :release_date , :website_url ,:email, license: {date1: :date1, license1: :license1, date2: :date2, license2: :license2, date3: :date3, license3: :license3, date4: :date4, license4: :license4})

and in form

= form_for @company_detail, remote: true, html: {class: 'form-horizontal form-label-left', data: {"parsley-validate" => ""}} do |f|
      = f.fields_for :license do |l|
        .row
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Date 1
              .col-md-8.col-xs-12
                = l.text_field :date1, value: @company_detail.license.present? ? @company_detail.license["date1"] : '', class: 'date-picker form-control has-feedback-left datePicker'
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Licence Number 1
              .col-md-8.col-xs-12
                = l.text_field :license1, value: @company_detail.license.present? ? @company_detail.license["license1"] : '', class: 'form-control'

I tried entering a few fields in the form and submit, but even though the params are going in correctly, the value saved is null. This is the log:

Parameters: {"utf8"=>"✓", "company_detail"=>{"license"=>{"date1"=>"121212", "license1"=>"12313", "date2"=>"122131123", "license2"=>"123123", "date3"=>"", "license3"=>"", "date4"=>"", "license4"=>""}}, "commit"=>"Save", "id"=>"1"}
CompanyDetail Load (0.3ms)  SELECT  "company_details".* FROM "company_details" WHERE "company_details"."id" = $1 LIMIT 1  [["id", 1]]
 User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
 (1.0ms)  BEGIN
SQL (0.4ms)  UPDATE "company_details" SET "license" = $1, "updated_at" = $2 WHERE "company_details"."id" = $3  [["license", "{"date1":null,"license1":null,"date2":null,"license2":null,"date3":null,"license3":null,"date4":null,"license4":null}"], ["updated_at", "2016-05-01 12:15:44.918547"], ["id", 1]]

ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(1.0ms) BEGIN
SQL (0.4ms) UPDATE "company_details" SET "license" =

I'm using rails 4.2 with ruby 2.2.
I'm trying to save some data inside a column in a hash format.

I'm trying to save license details in the license column which is of text datatype, and in the respective model i've given:

serialize :license, JSON

I have this in the controller:

params.require(:company_detail).permit(:company_name, :trading_name, :contact_name, :acn, :address, :suburb, :state, :post_code, :phone_number, :fax_number, :nrma_approved, :start_date, :release_date , :website_url ,:email, license: {date1: :date1, license1: :license1, date2: :date2, license2: :license2, date3: :date3, license3: :license3, date4: :date4, license4: :license4})

and in form

= form_for @company_detail, remote: true, html: {class: 'form-horizontal form-label-left', data: {"parsley-validate" => ""}} do |f|
      = f.fields_for :license do |l|
        .row
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Date 1
              .col-md-8.col-xs-12
                = l.text_field :date1, value: @company_detail.license.present? ? @company_detail.license["date1"] : '', class: 'date-picker form-control has-feedback-left datePicker'
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Licence Number 1
              .col-md-8.col-xs-12
                = l.text_field :license1, value: @company_detail.license.present? ? @company_detail.license["license1"] : '', class: 'form-control'

I tried entering a few fields in the form and submit, but even though the params are going in correctly, the value saved is null. This is the log:

Parameters: {"utf8"=>"✓", "company_detail"=>{"license"=>{"date1"=>"121212", "license1"=>"12313", "date2"=>"122131123", "license2"=>"123123", "date3"=>"", "license3"=>"", "date4"=>"", "license4"=>""}}, "commit"=>"Save", "id"=>"1"}
CompanyDetail Load (0.3ms)  SELECT  "company_details".* FROM "company_details" WHERE "company_details"."id" = $1 LIMIT 1  [["id", 1]]
 User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
 (1.0ms)  BEGIN
SQL (0.4ms)  UPDATE "company_details" SET "license" = $1, "updated_at" = $2 WHERE "company_details"."id" = $3  [["license", "{"date1":null,"license1":null,"date2":null,"license2":null,"date3":null,"license3":null,"date4":null,"license4":null}"], ["updated_at", "2016-05-01 12:15:44.918547"], ["id", 1]]

, "updated_at" = WHERE "company_details"."id" = [["license", "{"date1":null,"license1":null,"date2":null,"license2":null,"date3":null,"license3":null,"date4":null,"license4":null}"], ["updated_at", "2016-05-01 12:15:44.918547"], ["id", 1]]

2 ответа

  1. Попробуйте это для ваших сильных параметров для licence:

    license: [:date1, :license1, :date2, :license2, :date3, :license3, :date4, :license4]

  2. Если в качестве базы данных используется postgres, то можно использовать jsonв качестве типа данных для столбца.

    Смотрите здесь-ActiveRecord JSON

    Если вы изучаете запрос полей json, вы должны использовать это jsonbполе. Ссылка

    Но обратите вниманиеjsonb, что datatype был представлен только в postgresql v9.4