Rails nested attributes not saving using carrierwave

浪子不回头ぞ 提交于 2019-12-12 04:45:31

问题


I can get the gallery attributes to insert as evident by server log below but picture attributes will not insert as well.

Server response

Started POST "/galleries" for 127.0.0.1 at 2017-05-13 18:19:23 +1000
Processing by GalleriesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==", "gallery"=>{"name"=>"Hello", "cover"=>"123456", "picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50 @tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>, @original_filename="Skateboard 1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\"; filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"}
Unpermitted parameter: picture
   (0.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "galleries" ("name", "cover", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "Hello"], ["cover", 123456], ["created_at", 2017-05-13 08:19:23 UTC], ["updated_at", 2017-05-13 08:19:23 UTC]]
   (65.1ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 74ms (ActiveRecord: 66.1ms)

Started GET "/" for 127.0.0.1 at 2017-05-13 18:19:23 +1000
....

GalleriesController

class GalleriesController < ApplicationController

  def new
    @gallery = Gallery.new
  end

  def create
    @gallery = Gallery.new(gallery_params)
    if @gallery.save       
      flash[:success] = "Picture created!"
      redirect_to root_url
    else
      render 'new'
    end
  end

private

    def gallery_params
        params.require(:gallery).permit(:id, :name, :cover, pictures_attributes: [:id, :gallery_id, :picture, :_destroy])
      end
    end

_form.html.erb partial rendered from within new.html.erb

<%= form_for @gallery do |f| %>
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :cover %>
    <%= f.text_field :cover %>
  </div>
  <div id="pictures">
    <%= f.fields_for @gallery.pictures do |pic| %>
      <%= pic.file_field :picture %>
  </div>
    <% end %>
  <div id="submit">
    <%= f.submit %>
  </div>
<% end %>

Models, Gallery

class Gallery < ApplicationRecord
  has_many :pictures
  validates :name, presence: true
  validates :cover, presence: true
  accepts_nested_attributes_for :pictures, allow_destroy: true
end

Picture

 class Picture < ApplicationRecord
  belongs_to :gallery
  validates :gallery_id, presence: true
  validates :picture, presence: true
  mount_uploader :picture, PictureUploader
  serialize :picture, JSON
end

Migrations, Gallery

class CreateGalleries < ActiveRecord::Migration[5.0]
  def change
    create_table :galleries do |t|
      t.string :name
      t.integer :cover

      t.timestamps
    end
  end
end

Picture

class CreatePictures < ActiveRecord::Migration[5.0]
  def change
    create_table :pictures do |t|
      t.integer :gallery_id
      t.string :picture

      t.timestamps
    end
  end
end

回答1:


Unpermitted parameter: picture

The error is because your fields_for is wrong. The first parameter of fields_for should be a record_name(which should be :pictures in your case).

fields_for(record_name, record_object = nil, options = {}, &block)

You are passing record_object as a first parameter, which resulting in wrong params and leading to unpermitted error. Changing your code to below should resolve the issue.

<%= f.fields_for :pictures, @gallery.pictures do |pic| %>
  <%= pic.file_field :picture %>
<% end %>



回答2:


Judging by your parameters line here:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==",
"gallery"=>{"name"=>"Hello", "cover"=>"123456", 
"picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50 
@tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>, 
@original_filename="Skateboard 1.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\"; 
filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"}

and the fact you're getting the result: Unpermitted parameter: picture, you should change your strong parameters to

def gallery_params
    params.require(:gallery).permit(:id, :name, :cover, picture: [:id, :gallery_id, :picture, :_destroy])
end


来源:https://stackoverflow.com/questions/43951335/rails-nested-attributes-not-saving-using-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!