More on RestController and some updates 2

Posted by Tim Connor Fri, 20 Apr 2007 18:58:00 GMT

I’ve fleshed out RestController, through some more usage. Combined with moving associated object creation into the model, it now really DRYs out my code, by being a little more flexible.

On any overridden action you can call super, passing in an option options hash. The current options are :template, which works for the editing template for errors on update and create and :find_options, for sorting the index view, for instance. As well, the instance variables (@page_title and @your_model_name) are set with ||=, so you can do custom init and then call super.

Well a picture is worth a thousand words, so, here are the public methods, using all of these techniques, from my most complicated controller on a project using it:


def index(options = {})
super ({:find_options => {:order => ‘date DESC’}}.merge(options))
end

def public_index @page_title = ‘Sun Valley Fishing Report’ @body_class = ‘archive’ index :template => ‘public_index’ end def current @report = Report.current @page_title = ‘Latest Sun Valley Fishing Report’ public_show end def public_show @report ||= Report.find_by_date(params[:date]) @page_title ||= ‘Sun Valley Fishing Report’ render :text => ‘No report found’, :layout => ‘public’ and return unless @report show :template => ‘public_show’ end def new @report = Report.new() Location.find(:all).each { |location| @report.conditions.build(:location => location) } super end def edit @report = Report.find(params[:id]) Location.find(:all).each { |location| @report.conditions.build(:location => location) unless @report.locations.include?(location) } super end

Most of the other controllers ones are a couple of lines, maybe over-riding one action. For instance, here is my UserController, and even this might be simplified when I add in an error condition on edit and show (and/or an passed in error handling block):


class UsersController < REST::RestController
def edit
user = User.find_by_id(params[:id]) unless @user flash[:notice] = @messages[:editing_invalid_id]
redirect_to users_path and return
end
super
end
end

UPDATE: Same day – I updated again, and now I can empty out that user controller, because the edit and show actions have basic checks for not found.

Comments

Leave a comment

  1. Avatar
    roywblack 3 days later:

    Thanks Tim.

    This is very helpful if are doing a lot of REST stuff that is straightforward.

    I am curious if you have an example of your edit template that deals with new and edit.

  2. Avatar
    Tim Connor 3 days later:

    Roy, I’m using SimplyHelpful, which takes care of most of the complexities of new vs.edit, so it’s pretty straight-forward – but I’m also using Markaby, instead of Erb, so it won’t look quite like you are used to, most likely.

    I’ll use an example from a simpler model, because that one is a little longer than needed for demonstration purposes.

    views/locations/edit.mab


    error_messages_for ‘location’
    form_for(location) do |f| label 'Name - warning - changes existing reports', :for => 'location_name' text f.text_field(:name) label 'Closing Date (mm/dd/yyyy)', :for => 'location_closing_date' text f.text_field(:closing_date) text f.submit(location.new_record? ? ‘Save’ : ‘Update’)
    end
    p {link_to ‘Back to Location list’, locations_path.to_s}

Comments