Skip to content

Instantly share code, notes, and snippets.

Created January 19, 2010 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/280611 to your computer and use it in GitHub Desktop.
Save anonymous/280611 to your computer and use it in GitHub Desktop.
# partial that's inserted into pages new and pages edit
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :admin? %><br />
<%= f.check_box :admin %>
</p>
<p>
<%= f.label :parent_id %><br />
<%= f.collection_select :parent_id, Page.find(:all), :id, :navlabel, :include_blank => true %>
</p>
<p>
<%= f.label :position %><br />
<%= f.text_field :position, :size => '3' %>
</p>
<p>
<%= f.label :navlabel %><br />
<%= f.text_field :navlabel %>
</p>
<p>
<%= f.label :layout %><br />
<%= f.select :layout,layout_collect, {} %>
</p>
# pages edit
<h1>Editing page</h1>
<% form_for(@object) do |f| %>
<%= f.error_messages %>
<%= render :partial => 'form', :locals => {:f => f} %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Back', pages_path %>
class GlobalRestController < ApplicationController
def index
@objects = params[:controller].singularize.camelize.constantize.all
respond_to do |format|
format.html
format.xml { render :xml => @objects }
end
end
def show
@object = params[:controller].singularize.camelize.constantize.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @object }
end
end
def new
@object = params[:controller].singularize.camelize.constantize.new
respond_to do |format|
format.html
format.xml { render :xml => @object }
end
end
def edit
@object = params[:controller].singularize.camelize.constantize.find(params[:id])
end
def create
model = params[:controller].singularize.downcase
@object = params[:controller].singularize.camelize.constantize.new(params[model])
respond_to do |format|
if @object.save
flash[:notice] = "#{params[:controller].singularize.camelize} was successfully created."
format.html { redirect_to(@object) }
format.xml { render :xml => @object, :status => :created, :location => @object }
else
format.html { render :action => "new" }
format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
end
end
end
def update
model = params[:controller].singularize.downcase
@object = params[:controller].singularize.camelize.constantize.find(params[:id])
respond_to do |format|
if @object.update_attributes(params[model])
flash[:notice] = "#{params[:controller].singularize.camelize} was successfully updated."
format.html { redirect_to(@object) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@object = params[:controller].singularize.camelize.constantize.find(params[:id])
@object.destroy
respond_to do |format|
format.html { redirect_to(@object) }
format.xml { head :ok }
end
end
end
# if you have a controller called pages, you would substitute the instance variable @pages with @objects
<table>
<% @objects.each do |page| %>
<tr class="oddrow">
<td class="leftAlign"><%=h page.name %></td>
<td class="leftAlign"><%=h page.navlabel %></td>
<td class="centerAlign"><%=h page.position %></td>
<td class="centerAlign"><%=h page.layout %></td>
<td class="centerAlign"><%=h page.admin? ? "Yes" : "No" %></td>
<td class="centerAlign"><%=h page.parent_id != nil ? "Yes" : "No" %></td>
<td class="centerAlign"><%= link_to "Show", :alt => "Show"), page %></td>
<td class="centerAlign"><%= link_to "Edit", :alt => "Edit"), edit_page_path(page) %></td>
<td class="centerAlign"><%= link_to "Destroy", :alt => "Destroy"), page, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
# pages new
<h1>New page</h1>
<% form_for(@object) do |f| %>
<%= f.error_messages %>
<%= render :partial => 'form', :locals => {:f => f} %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', pages_path %>
class PagesController < GlobalRestController
#nothing here needed unless you decide to overwrite a rest method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment