【问题标题】:Unpermitted parameters: image.Carrierwave不允许的参数:image.Carrierwave
【发布时间】:2015-01-20 21:13:52
【问题描述】:

我尝试将“carrierwave”gem 添加到我的应用中。我创建了一个类 image_uploader

class ImageUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

并创建一个模型photo.rb

class Photo < ActiveRecord::Base
  mount_uploader :image, ImageUploader
  belongs_to :photoable, polymorphic: true
  belongs_to :post
end

并且 photo.rbpost.rb

具有多态关联
class Post < ActiveRecord::Base
  has_many :comments
  has_many :post_attachments
  validates :title, :body, presence: true
  has_many :photos, as: :photoable
  accepts_nested_attributes_for :photos
end

post.rb 有一个来自 photo.rb 的嵌套属性

  params.require(:post).permit(:title, :body, photo_attributes: [:image])

这是来自 post.rb 模型的部分 _form.html.haml

= form_for [:admin, @post] do |f|
  = f.file_field :image
  = f.text_field :title, class: "form-control", placeholder: "Title"
  = f.text_area :body, rows: 12, class: "form-control", placeholder: "Message"
  .pull-right
    = f.submit "Send", class: "btn btn-success"

但是当我用图片创建帖子时,shell 会显示一个错误

Unpermitted parameters: image

如何解决?

对不起我的英语不好

UPD

schema.rb

create_table "photos", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image"
    t.integer  "photoable_id"
    t.string   "photoable_type"
  end

  create_table "posts", force: true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

已解决

【问题讨论】:

    标签: ruby-on-rails carrierwave nested-attributes polymorphic-associations


    【解决方案1】:

    我认为应该是photos_attributes

    params.require(:post).permit(:title, :body, photo_attributes: [:image])
                                                    ^^
    

    【讨论】:

    • 谢谢你的回答,但这个解决方案不起作用。Сause of else
    【解决方案2】:

    问题是您没有将fields_for 用于image 属性。

    你想要的不是= f.file_field :image

    = f.fields_for :photos do |photo_fields| %>
       = photo_fields.file_field :image
    

    然后,将params.require(:post).permit(:title, :body, photo_attributes: [:image]) 更改为:

    params.require(:post).permit(:title, :body, photos_attributes: [:image])

    【讨论】:

    • 谢谢,我确实喜欢这个pastie.org/9846945#,但出现错误“未经允许的参数:照片”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多