Skip to content

Instantly share code, notes, and snippets.

@mrmemes-eth
Forked from jens/attached_file.rb
Created May 17, 2011 20:44
Show Gist options
  • Save mrmemes-eth/977349 to your computer and use it in GitHub Desktop.
Save mrmemes-eth/977349 to your computer and use it in GitHub Desktop.
Duplicate items when using update_attributes and decent_exposure
class AttachedFile < ActiveRecord::Base
belongs_to :parent, :polymorphic => true
end
class Event < ActiveRecord::Base
has_many :attached_files, :as => :parent
accepts_nested_attributes_for :attached_files, :allow_destroy => true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Admin::EventsController < ApplicationController
respond_to :html
expose(:events) { Event.all }
expose(:event)
def create
event.save
respond_with(:admin, event)
end
def update
event.update_attributes(params[:event])
respond_with(:admin, event)
end
end
<% form_for [:admin, event] do |f| %>
<%= f.text_field :name %>
<%- f.fields_for :attached_files, event.attached_files.build do |a| %>
<%= a.text_field :name %>
<% end %>
<%= f.submit %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment