Who Needs The Show Action Anyway
We all know that Rails has seven standard REST actions, index, show, new, create, edit, update and delete. Usually there are only views associated with four of them, index, show, new and edit with new and edit usually being the same. This all makes perfect sense for the public facing part of the application, but what about the administration portion of your app? Do you really need that show action? Are you going to be displaying the information in the same way as the public facing side of things? I’m starting to think that skipping the show action in the administration portion of the application is not a bad idea.
Context Is Key
One thing that I’ve noticed with your average “show” action in the administration portion of the web app is that it ignores the context in which the administrator wants to see the data. Rather than displaying what the public user will see, the admin show action is usually just a basic display of the information from the record.
For example, say you’ve got a content page that you have the ability to edit through the administration area. When you’re done editing it, you want to check the formatting, colors, links and general look of the page. Unless you’re using the same CSS styles and layouts for the public facing portion of the site and the administration portion of the site, you’re not going to be able to get a good idea of what it’s really going to look like. Sure you could recreate the public facing styles and part of the layout in the administration area but that’s jumping through a lot of hoops and getting out of DRY land.
If the information that you’re displaying isn’t view/layout dependent (like a user record) then there is probably no harm in having a simple show view that looks different in the administration area and the public facing area. However, if the information ends up being styled and displayed differently between the two areas, it might be worth for going the show action in the administration area.
How Rails Can Help
You’ve decided that it’s important to always view the public facing version of a resources show action when you’re in the administration area, luckily Rails makes it pretty easy to implement this functionality.
1. Add a new member route to your existing resource
map.resources :pages, :member => { :preview => :get }
2. Add the new preview action to your controller
class PagesController < ApplicationController
# ...
def preview
@page = Page.find(params[:id])
end
# ...
3. Tell your action which layout to use
class PagesController < ApplicationController
# ...
def preview
@page = Page.find(params[:id])
render :action => "pages/show", :layout => "public"
end
# ...
4. Add your new “Preview” link somewhere in your Administration area
# show.html.erb in app/views/admin/pages/show.html.erb
<%= link_to("Preview", preview_page_url(@page), :target => "_blank") %>
This assumes that you’ve got the public facing show template in app/views/pages/show and a layout file named “public”.
Now you can edit your records in the administration area but view them in the context that makes the most sense. While I might not do this to something like a user record or order receipt, this is definitely a good idea for blog posts, content pages and other administrable content.











