Topic: Cleaner way to use shared templates

I'm using shared templates for a STI model.

The controller is starting to get littered with:
      render :template => 'shared/topics/edit'

For example:
  def edit
    @topic = Topic.find(params[:id])
    if request.post?
      if @topic.update_attributes(params[:topic])
        flash[:notice] = @topic.name + ' was successfully edited.'
        redirect_to :action => 'manage'
      else
        render :template => 'shared/topics/edit'
      end
    else
      render :template => 'shared/topics/edit'
    end
  end

And I was wondering if there's a way to set the default template (not the layout but the template) for the 'edit' action.  I could use routes, but I don't particularly want users to go to http://WEBSITE/shared/topics/edit - rather http://WEBSITE/news/edit.

It certainly seems like something you could do with rails, but I've been looking through the ActionController API with no luck.

Re: Cleaner way to use shared templates

You can place the render template at the end and return if you do something else before then (such as redirecting:

  def edit
    @topic = Topic.find(params[:id])
    if request.post? && @topic.update_attributes(params[:topic])
      flash[:notice] = @topic.name + ' was successfully edited.'
      redirect_to :action => 'manage' and return
    end
    render :template => 'shared/topics/edit'
  end

I don't know of a good way to set the default, but I haven't looked either. Out of curiosity, why aren't you using the default edit.rhtml in the controller's view directory instead of a shared directory?

Railscasts - Free Ruby on Rails Screencasts

Re: Cleaner way to use shared templates

I'm using the shared to reduce duplication for my STI classes:
A topic can be of type "News" or "Discussion" for example.

I can create new types of Topics, use the shared views for most of the
actions (list, edit, etc.).

So far, I just keep "Show" as unique for each type.

Re: Cleaner way to use shared templates

Oh okay. Since it sounds like the subclasses have more in common than they do different, you may want to consider moving it all into one controller then use an if/case condition to change the behavior when needed.

Railscasts - Free Ruby on Rails Screencasts

Re: Cleaner way to use shared templates

So far the parent controller does all the work and the subclass controllers are empty.
I use params[:controller].singularize.capitalize to determine the type for messages back to the user: ex. "Are you sure you want to delete your " + params[:controller].singularize.capitalize + "?"

However, as the application grows I envision extending the subclasses and differentiating the way they interact with other classes in the application.

Am I doing what you're suggesting--the parent controller class doing all the work, or are you suggesting I blow the subclasses away altogether?

Re: Cleaner way to use shared templates

I see, the approach you took sounds good as there isn't any duplication (you only extend what is necessary). I was thinking each model subclass had a completely separate controller which isn't the case.

Railscasts - Free Ruby on Rails Screencasts

Re: Cleaner way to use shared templates

Great, thanks for the feedback