class <%=child_plural_class_name%>Controller < ApplicationController filter_access_to :index, :new, :create, :destroy filter_access_to :edit, :update, :attribute_check => true layout 'cms' def index @<%=child_plural_name%> = <%=child_class_name%>.page(params[:search], params[:page]) @<%=child_singular_name%>_<%=parent_plural_name%> = @<%=child_plural_name%>.group_by { |<%=child_singular_name%>| [<%=child_singular_name%>.<%=parent_singular_name%>.title] } end def show @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id], :include => :<%=parent_singular_name%>) @total_uploads = <%=child_class_name%>.find(:all, :conditions => { :<%=parent_singular_name%>_id => @<%=child_singular_name%>.<%=parent_singular_name%>.id}) @<%=parent_singular_name%> = @<%=child_singular_name%>.<%=parent_singular_name%> end def new @<%=child_singular_name%> = <%=child_class_name%>.new end def create newparams = coerce(params) @<%=child_singular_name%> = <%=child_class_name%>.new(newparams[:<%=child_singular_name%>]) if @<%=child_singular_name%>.save flash[:notice] = "Successfully created <%=child_singular_name%>." respond_to do |format| format.html { redirect_to @<%=child_singular_name%>.<%=parent_singular_name%> } format.json { render :json => { :result => 'success', :<%=child_singular_name%> => <%=child_singular_name%>_url(@<%=child_singular_name%>) } } end else render :action => 'new' end end def edit if params[:edit] == '<%=child_singular_name%>' render :layout => false @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id]) else @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id]) end end def update @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id]) if @<%=child_singular_name%>.update_attributes(params[:<%=child_singular_name%>]) flash[:notice] = "Successfully updated <%=child_singular_name%>." redirect_to <%=parent_singular_name%>_url(@<%=child_singular_name%>.<%=parent_singular_name%>) else render :action => 'edit' end end def destroy if request.post? if params[:<%=child_singular_name%>_ids] params[:<%=child_singular_name%>_ids].each do |<%=child_singular_name%>| @<%=child_singular_name%> = <%=child_class_name%>.find <%=child_singular_name%> @<%=child_singular_name%>.destroy end flash[:notice] = "<%=child_plural_class_name%> Successfully deleted!" end redirect_to(<%=parent_singular_name%>_path(@<%=child_singular_name%>.<%=parent_singular_name%>)) end end def array menu_sort(params[:ul]) flash[:notice] = "Successfully Sorted <%=child_plural_class_name%>." end private def coerce(params) if params[:<%=child_singular_name%>].nil? h = Hash.new h[:<%=child_singular_name%>] = Hash.new h[:<%=child_singular_name%>][:<%=parent_singular_name%>_id] = params[:<%=parent_singular_name%>_id] h[:<%=child_singular_name%>][:title] = params[:title] h[:<%=child_singular_name%>][:file] = params[:Filedata] h[:<%=child_singular_name%>][:file].content_type = MIME::Types.type_for(h[:<%=child_singular_name%>][:file].original_filename).to_s h else params end end end