dragonfly icon indicating copy to clipboard operation
dragonfly copied to clipboard

Can't Mass Assign Mongoid Nested Documents

Open rdetert opened this issue 11 years ago • 2 comments

Maybe it's user error, but I can't mass assign the image field in the embedded model PageSetting. This just stores a simple key/value pair and/or an image.

class PageSetting
  include Mongoid::Document
  extend Dragonfly::Model

  embedded_in :pages

  field :image_uid
  dragonfly_accessor :image

  field :key
  field :value

  before_save :strip_value

  def get_image
    return image.url if image
    value
  end

  def strip_value
    self.value.strip!
  end
end

class Page
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :page_settings
  accepts_nested_attributes_for :page_settings

  field :path
  field :name

  def setting(key)
    page_settings.where(key: key).first.value rescue nil
  end

  def setting_keys
    keys = []
    page_settings.each do |setting|
      keys << setting.key
    end
    keys
  end

end


class Admin::PagesController < Admin::AdminController 

  def index
    @pages = Page.all
  end

  def edit
    @page = Page.find(params[:id])
  end

  def update
    @page = Page.find(params[:id])

    if @page.update_attributes post_params   # Doesn't save
      redirect_to admin_pages_path, notice: 'Saved!'
    else
      redirect_to edit_admin_page_path(@page), alert: 'Error!'
    end
  end


protected

  def post_params
    safe_params = [ 
      :name, :path,
      page_settings_attributes: [
        :id, :key, :value, :image, :_destroy
      ] 
    ]
    params[:page].permit(*safe_params)  
  end

end

rdetert avatar Sep 01 '14 07:09 rdetert

sorry for the delay - did you ever figure this out?

markevans avatar Apr 23 '15 12:04 markevans

Getting this as well. Anyone found a solution?

elithecho avatar Jun 19 '17 16:06 elithecho