-1

我試圖添加到我的應用程序 'carrierwave' gem.I創建一個類image_uploader未經許可參數:image.Carrierwave

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.rb有多態性關聯post.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.rbphoto.rb

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

具有嵌套屬性,這是局部_form.html.hamlpost.rb模型

= 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 

解決

回答

0

我認爲應該是photos_attributes

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

感謝你的答案,但這個解決方案確實的其他 – vveare138 2015-01-20 21:27:55

0

的問題是,你不使用fields_forimage屬性。

而不是= 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])

+0

地方不work.Сause謝謝,我確實喜歡這個http://pastie.org/9846945#,但是錯誤'Unpermitted parameters:photo' – vveare138 2015-01-21 09:01:11

+0

啊,應該是'photos'。我更新了答案。 – mmichael 2015-01-21 15:34:48

相關問題