【问题标题】:Rails nested attributes not saving using carrierwaveRails嵌套属性不使用carrierwave保存
【发布时间】:2017-05-13 09:15:36
【问题描述】:

我可以让画廊属性插入如下服务器日志所示,但图片属性也不会插入。

服务器响应

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
....

画廊控制器

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 从 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 %>

模特、画廊

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

图片

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

迁移,画廊

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

      t.timestamps
    end
  end
end

图片

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 carrierwave


    【解决方案1】:

    不允许的参数:图片

    错误是因为您的fields_for 错误。 fields_for第一个参数 应该是 record_name(在您的情况下应该是 :pictures)。

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

    您将record_object 作为第一个参数传递,这会导致参数错误并导致未允许的错误强>。将代码更改为以下应该可以解决问题。

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

    【讨论】:

    • 感谢您的意见。你是对的,但是当我这样做时,页面上的图片消失了file_field,所以我必须这样做f.fields_for @gallery.pictures do.. 现在我意识到要获得允许的参数,我需要将该字段命名为name: "pictures[picture]",这样就可以摆脱错误,但这只会插入画廊属性而不是图片。所以要在控制器中处理这个问题,我需要添加 params[:pictures][:picture].each do |pic| 然后 @picture = @gallery.pictures.create(picture: pic) end 和所有工作:) 谢谢大家
    • 很抱歉,实际上会产生错误,而只是@picture = @gallery.pictures.create(picture: params[:pictures][:picture]),因为它是单张图片
    【解决方案2】:

    根据您的参数行判断:

    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,你应该将你的强参数更改为

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

    【讨论】:

    • 是的,但我认为它是针对图片属性的说法,所以错误一定是在其他地方?
    • @LeeEather 您当前允许参数哈希pictures_attributes,但您正在提交参数哈希picture
    • 我试过了,我得到了一个unknown attribute error picture for Gallery 我很确定你必须为表单中的参数指定你拥有的图片属性为picture_attributes,即使只是添加Picture 模型没有专门引用它们的属性仍然不起作用,所以必须是别的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多