2

我正在尝试使用 Accept_nested_attributes 通过 ActiveStorage 保存图像。当我编辑子记录时它工作正常,但是当我将它放在表单中时,它看起来一切正常,但它不是实际保存。贝娄是我目前正在使用的代码:

控制器:

strong_params = params.require(:inspection).permit(
        :name,
        :description,
        :schedule,
        :department_id,
        :active,
        :start_time,
        :recurring,
        :inspectionpoints_attributes => [ :id, :name, :description, :active, :question_type, :_destroy ],)

模型

has_many :inspectionpointactuals, class_name: 'Inspectionpointactual', dependent: :destroy, foreign_key: :inspectionactual_id
  accepts_nested_attributes_for :inspectionpointactuals, :allow_destroy => true

和表格

<%= form_with(model: inspectionactual, url: (inspectionactual.persisted? ? [:account, inspectionactual] : [:account, @inspection, :inspectionactuals]), local: true, :html => { :multipart => true }) do |form| %>
  <%= render 'shared/forms/errors', form: form %>


  <%= render 'shared/fields/date_and_time_field', form: form, method: :date, options: {autofocus: true} %>
   <%= form.hidden_field :user_id, :value => current_user.id %> 
  <%= render 'shared/fields/text_field', form: form, method: :name %>
  <%= render 'shared/fields/text_area', form: form, method: :description %>

    
   <h5>Indiviudal Points</h5>
 <%= form.fields_for :inspectionpointactuals do |point| %>
 <div class="element-box">
  <% if point.object.question_type == "boolean" %>
       <%= form.label point.object.name %><br/>
       <div>
      <label class="btn-toggle"> 
      <%= point.radio_button :response, "True", class: "btn" %><button type="button" class="btn">
            Yes
          </button>
    </label>
 
    <label class="btn-toggle"> 
      <%= point.radio_button :response, "False", class: "btn" %><button type="button" class="btn">
            No
          </button>
    </label>
     <label class="btn-toggle"> 
      <%= point.radio_button :response, "Not Inspected", class: "btn" %><button type="button" class="btn">
            Not Inspected
          </button>
    </label>
  </div>
<br/>
  <% elsif  point.object.question_type == "integer" %>
      <%= form.label point.object.name %>
      <%= point.number_field :response, class: "form-control" %>

  <% elsif point.object.question_type == "image"%>
      <%= form.label point.object.name %>
       <div class="fileUpload btn btn-primary">
        <!-- Image upload not working as has nested yet -->
        <%= point.file_field :image, class: "form-control" %> Upload
       </div>
  <%= point.file_field :image, class: "upload" %> Upload
  <% else %>
      <%= form.label point.object.name  %>
      <%= point.text_field :response, class: "form-control" %>
  <% end %>  
  </div> 
<% end %>



  <div class="form-buttons-w">
    <%= form.submit (form.object.persisted? ? t('.buttons.update') : t('.buttons.create')), class: "btn btn-primary" %>
    <% if form.object.persisted? %>
    <%= link_to t('global.buttons.cancel'), [:account, inspectionactual], class: "btn btn-link" %>
    <% else %>
    <%= link_to t('global.buttons.cancel'), [:account, @inspection, :inspectionactuals], class: "btn btn-link" %>
    <% end %>
  </div>

<% end %>

我最近在表单中添加了“, :html => { :multipart => true }”,因为它是从类似的 StackOverflow 推荐的,但这并没有成功。我的控制台数据看起来可以正常工作,在我修复控制器之前,我收到了被拒绝的保存,但现在它似乎采用了 :image。

Processing by Account::InspectionactualsController#update as HTML
  Parameters: {"authenticity_token"=>"g48sA3QkBqOrNmz4kCaIgIndv3EHqnMHkdnM7X+v5Qc8s4o5BpH+DHSscjkl+Xb8qBVH9hPsabRxzvdKMQK0rw==", "inspectionactual"=>{"date"=>"06/25/2020", "user_id"=>"1", "name"=>"now", "description"=>"testin image", "inspectionpointactuals_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f8a21da8718 @tempfile=#<Tempfile:/var/folders/_3/rc02kyfx3hv9ct02ymhb33rc0000gn/T/RackMultipart20200623-35070-1g0wg9g.png>, @original_filename="Screen Shot 2020-06-18 at 11.28.48 AM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"inspectionactual[inspectionpointactuals_attributes][0][image]\"; filename=\"Screen Shot 2020-06-18 at 11.28.48 AM.png\"\r\nContent-Type: image/png\r\n">, "id"=>"14"}}}, "commit"=>"Update Inspectionactual", "id"=>"6"}

任何帮助将不胜感激,我觉得我只是缺少一些简单的东西。

根据开发日志的要求。第一个是 has_nested 不起作用的那个。第二个是工作的。

Rendered /Users/evanliewer/.rvm/gems/ruby-2.6.4/gems/xray-rails-0.3.2/app/views/_xray_bar.html.erb (Duration: 8.0ms | Allocations: 7416)
Unsubscribing from channel: {"channel":"Sprinkles::CollectionsChannel","collection_name":"inspectionpointactuals"}
Started PATCH "/account/inspectionactuals/6" for ::1 at 2020-06-26 16:50:17 -0700
Processing by Account::InspectionactualsController#update as HTML
  Parameters: {"authenticity_token"=>"bJVUGe87RQDpfolpTGTOK0JfDGBSKldUx8KnxHlHl+DvUzvZgcl+rr0UgCjPFVBqUYx6mF680gBCYimG8sS/4w==", "inspectionactual"=>{"date"=>"06/25/2020", "user_id"=>"1", "name"=>"now", "description"=>"testin image", "inspectionpointactuals_attributes"=>{"0"=>{"response"=>"", "id"=>"16"}, "1"=>{"id"=>"15"}, "2"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f8a2c20aa68 @tempfile=#<Tempfile:/var/folders/_3/rc02kyfx3hv9ct02ymhb33rc0000gn/T/RackMultipart20200626-35070-5fr3au.png>, @original_filename="dinner.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"inspectionactual[inspectionpointactuals_attributes][2][image]\"; filename=\"dinner.png\"\r\nContent-Type: image/png\r\n">, "id"=>"14"}}}, "commit"=>"Update Inspectionactual", "id"=>"6"}
  [1m[36mUser Load (0.3ms)[0m  [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mInspectionactual Load (0.3ms)[0m  [1m[34mSELECT "inspectionactuals".* FROM "inspectionactuals" WHERE "inspectionactuals"."id" = $1 LIMIT $2[0m  [["id", 6], ["LIMIT", 1]]
  [1m[36mInspection Load (0.3ms)[0m  [1m[34mSELECT "inspections".* FROM "inspections" WHERE "inspections"."id" = $1 LIMIT $2[0m  [["id", 4], ["LIMIT", 1]]
  [1m[36mTeam Load (0.2ms)[0m  [1m[34mSELECT "teams".* FROM "teams" WHERE "teams"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/account/application_controller.rb:46:in `load_team'
  [1m[36mUser Update (0.8ms)[0m  [1m[33mUPDATE "users" SET "current_team_id" = $1 WHERE "users"."id" = $2[0m  [["current_team_id", 1], ["id", 1]]
  ↳ app/controllers/account/application_controller.rb:56:in `load_team'
  [1m[35m (0.2ms)[0m  [1m[35mBEGIN[0m
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
  [1m[36mInspectionpointactual Load (0.5ms)[0m  [1m[34mSELECT "inspectionpointactuals".* FROM "inspectionpointactuals" WHERE "inspectionpointactuals"."inspectionactual_id" = $1 AND "inspectionpointactuals"."id" IN ($2, $3, $4)[0m  [["inspectionactual_id", 6], ["id", 16], ["id", 15], ["id", 14]]
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
  [1m[36mUser Load (0.3ms)[0m  [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
  [1m[36mInspectionactual Load (0.2ms)[0m  [1m[34mSELECT "inspectionactuals".* FROM "inspectionactuals" WHERE "inspectionactuals"."id" = $1 LIMIT $2[0m  [["id", 6], ["LIMIT", 1]]
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
  [1m[36mWebhooks::Outgoing::EventType Load (0.2ms)[0m  [1m[34mSELECT "webhooks_outgoing_event_types".* FROM "webhooks_outgoing_event_types" WHERE "webhooks_outgoing_event_types"."name" = $1 LIMIT $2[0m  [["name", "inspectionactual.updated"], ["LIMIT", 1]]
  ↳ app/models/concerns/webhooks/outgoing/issuing_model.rb:22:in `generate_webhook'
  [1m[36mInspectionpointactual Update (0.4ms)[0m  [1m[33mUPDATE "inspectionpointactuals" SET "response" = $1, "updated_at" = $2 WHERE "inspectionpointactuals"."id" = $3[0m  [["response", ""], ["updated_at", "2020-06-26 23:50:17.600405"], ["id", 16]]
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
  [1m[36mWebhooks::Outgoing::EventType Load (0.2ms)[0m  [1m[34mSELECT "webhooks_outgoing_event_types".* FROM "webhooks_outgoing_event_types" WHERE "webhooks_outgoing_event_types"."name" = $1 LIMIT $2[0m  [["name", "inspectionpointactual.updated"], ["LIMIT", 1]]
  ↳ app/models/concerns/webhooks/outgoing/issuing_model.rb:22:in `generate_webhook'
  [1m[36mInspectionpoint Load (0.3ms)[0m  [1m[34mSELECT "inspectionpoints".* FROM "inspectionpoints" WHERE "inspectionpoints"."inspection_id" = $1[0m  [["inspection_id", 4]]
  ↳ app/models/inspectionactual.rb:33:in `create_inpectionpoints'
  [1m[36mInspectionpointactual Load (0.3ms)[0m  [1m[34mSELECT "inspectionpointactuals".* FROM "inspectionpointactuals" WHERE "inspectionpointactuals"."inspectionactual_id" = $1 AND "inspectionpointactuals"."name" = $2 AND "inspectionpointactuals"."description" = $3 AND "inspectionpointactuals"."question_type" = $4 AND "inspectionpointactuals"."sort_order" = $5 LIMIT $6[0m  [["inspectionactual_id", 6], ["name", "Third time"], ["description", "more over boo"], ["question_type", "boolean"], ["sort_order", 2], ["LIMIT", 1]]
  ↳ app/models/inspectionactual.rb:34:in `block in create_inpectionpoints'
  [1m[36mInspectionpointactual Load (0.4ms)[0m  [1m[34mSELECT "inspectionpointactuals".* FROM "inspectionpointactuals" WHERE "inspectionpointactuals"."inspectionactual_id" = $1 AND "inspectionpointactuals"."name" = $2 AND "inspectionpointactuals"."description" = $3 AND "inspectionpointactuals"."question_type" = $4 AND "inspectionpointactuals"."sort_order" = $5 LIMIT $6[0m  [["inspectionactual_id", 6], ["name", "image"], ["description", "image"], ["question_type", "image"], ["sort_order", 0], ["LIMIT", 1]]
  ↳ app/models/inspectionactual.rb:34:in `block in create_inpectionpoints'
  [1m[36mInspectionpointactual Load (0.3ms)[0m  [1m[34mSELECT "inspectionpointactuals".* FROM "inspectionpointactuals" WHERE "inspectionpointactuals"."inspectionactual_id" = $1 AND "inspectionpointactuals"."name" = $2 AND "inspectionpointactuals"."description" = $3 AND "inspectionpointactuals"."question_type" = $4 AND "inspectionpointactuals"."sort_order" = $5 LIMIT $6[0m  [["inspectionactual_id", 6], ["name", "Second"], ["description", "this num"], ["question_type", "integer"], ["sort_order", 1], ["LIMIT", 1]]
  ↳ app/models/inspectionactual.rb:34:in `block in create_inpectionpoints'
  [1m[35m (0.5ms)[0m  [1m[35mCOMMIT[0m
  ↳ app/controllers/account/inspectionactuals_controller.rb:44:in `block in update'
Redirected to http://localhost:3000/account/inspectionactuals/6
Completed 302 Found in 30ms (ActiveRecord: 5.7ms | Allocations: 20442)

和工作的那个。

Rendered /Users/evanliewer/.rvm/gems/ruby-2.6.4/gems/xray-rails-0.3.2/app/views/_xray_bar.html.erb (Duration: 7.1ms | Allocations: 7416)
Unsubscribing from channel: {"channel":"Sprinkles::CollectionsChannel","collection_name":"inspectionpointactuals"}
Started PATCH "/account/inspectionpointactuals/14" for ::1 at 2020-06-26 16:51:19 -0700
Processing by Account::InspectionpointactualsController#update as HTML
  Parameters: {"authenticity_token"=>"vfob+2B1WXl7WJMcaHcpRyfYNjlwBhPaiHOxz1y1GkGDR38xPYxsFyBSPC4LDcOsgNKM3Y1+ZvnQrI4sRySgpw==", "inspectionpointactual"=>{"name"=>"image", "description"=>"image", "response"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f8a2c131ec0 @tempfile=#<Tempfile:/var/folders/_3/rc02kyfx3hv9ct02ymhb33rc0000gn/T/RackMultipart20200626-35070-1c5i4ng.png>, @original_filename="lunch.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"inspectionpointactual[image]\"; filename=\"lunch.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update Inspectionpointactual", "id"=>"14"}
  [1m[36mUser Load (0.4ms)[0m  [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mInspectionpointactual Load (0.4ms)[0m  [1m[34mSELECT "inspectionpointactuals".* FROM "inspectionpointactuals" WHERE "inspectionpointactuals"."id" = $1 LIMIT $2[0m  [["id", 14], ["LIMIT", 1]]
  [1m[36mInspectionactual Load (0.3ms)[0m  [1m[34mSELECT "inspectionactuals".* FROM "inspectionactuals" WHERE "inspectionactuals"."id" = $1 LIMIT $2[0m  [["id", 6], ["LIMIT", 1]]
  [1m[36mInspection Load (0.2ms)[0m  [1m[34mSELECT "inspections".* FROM "inspections" WHERE "inspections"."id" = $1 LIMIT $2[0m  [["id", 4], ["LIMIT", 1]]
  [1m[36mTeam Load (0.4ms)[0m  [1m[34mSELECT "teams".* FROM "teams" WHERE "teams"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/account/application_controller.rb:46:in `load_team'
  [1m[36mUser Update (0.9ms)[0m  [1m[33mUPDATE "users" SET "current_team_id" = $1 WHERE "users"."id" = $2[0m  [["current_team_id", 1], ["id", 1]]
  ↳ app/controllers/account/application_controller.rb:56:in `load_team'
  [1m[35m (0.2ms)[0m  [1m[35mBEGIN[0m
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mInspectionpointactual Update (0.4ms)[0m  [1m[33mUPDATE "inspectionpointactuals" SET "response" = $1, "updated_at" = $2 WHERE "inspectionpointactuals"."id" = $3[0m  [["response", ""], ["updated_at", "2020-06-26 23:51:19.219950"], ["id", 14]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mWebhooks::Outgoing::EventType Load (0.2ms)[0m  [1m[34mSELECT "webhooks_outgoing_event_types".* FROM "webhooks_outgoing_event_types" WHERE "webhooks_outgoing_event_types"."name" = $1 LIMIT $2[0m  [["name", "inspectionpointactual.updated"], ["LIMIT", 1]]
  ↳ app/models/concerns/webhooks/outgoing/issuing_model.rb:22:in `generate_webhook'
  [1m[36mActiveStorage::Blob Load (1.3ms)[0m  [1m[34mSELECT "active_storage_blobs".* FROM "active_storage_blobs" INNER JOIN "active_storage_attachments" ON "active_storage_blobs"."id" = "active_storage_attachments"."blob_id" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4[0m  [["record_id", 14], ["record_type", "Inspectionpointactual"], ["name", "image"], ["LIMIT", 1]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mActiveStorage::Attachment Load (0.4ms)[0m  [1m[34mSELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4[0m  [["record_id", 14], ["record_type", "Inspectionpointactual"], ["name", "image"], ["LIMIT", 1]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mActiveStorage::Blob Create (1.0ms)[0m  [1m[32mINSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"[0m  [["key", "3u4gi2yl12sgezxosjcyjqn3jcnq"], ["filename", "lunch.png"], ["content_type", "image/png"], ["metadata", "{\"identified\":true}"], ["byte_size", 330705], ["checksum", "gEmvN+TohyaBrLnM4b2PKg=="], ["created_at", "2020-06-26 23:51:19.232257"]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mActiveStorage::Attachment Create (0.9ms)[0m  [1m[32mINSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m  [["name", "image"], ["record_type", "Inspectionpointactual"], ["record_id", 14], ["blob_id", 7], ["created_at", "2020-06-26 23:51:19.235727"]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[36mInspectionpointactual Update (1.0ms)[0m  [1m[33mUPDATE "inspectionpointactuals" SET "updated_at" = $1 WHERE "inspectionpointactuals"."id" = $2[0m  [["updated_at", "2020-06-26 23:51:19.238212"], ["id", 14]]
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
  [1m[35m (0.5ms)[0m  [1m[35mCOMMIT[0m
  ↳ app/controllers/account/inspectionpointactuals_controller.rb:42:in `block in update'
[36m  Disk Storage (2.9ms) [0m[32mUploaded file to key: 3u4gi2yl12sgezxosjcyjqn3jcnq (checksum: gEmvN+TohyaBrLnM4b2PKg==)[0m
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 4cd01694-fd76-4baf-8cb7-d3feabdd9898) to Sidekiq(active_storage_analysis) with arguments: #<GlobalID:0x00007f8a2c20a838 @uri=#<URI::GID gid://campfire/ActiveStorage::Blob/7>>
Redirected to http://localhost:3000/account/inspectionpointactuals/14
Completed 302 Found in 45ms (ActiveRecord: 8.5ms | Allocations: 23978)
4

1 回答 1

0

我能够在 accept_nested_attributes 上找到一个能够解决问题的拉取请求。解决方案的链接在这里https://github.com/rails/rails/issues/37701

我添加到子模型的代码是这样的:

 def changed_for_autosave?
      if image.present?
    super || image.changed_for_autosave?
      end
  end
于 2020-06-27T00:41:36.130 回答