Camping: return users to the latest posts, but keep mistakes

Users can view a specific entry in my web app with a url. /entry/8

, eg. If the entry does not exist, "Entry not found" is appended to @messages

and I generate an error page.

I would like to show an arbitrary request instead of a blank page, but I cannot find a good way to store the error message for display. There are other things that need to be done in an arbitrary request controller, so I can't just duplicate the request and render :posts

.

Sample code:

module MyApp :: Controllers
  class ComplexQuery <R '/ query'
    def get
      @entries = Entries.all (: conditions => someComplexConditions)
      until @ entries.complexEnough? then @ entries.makeMoreComplex! end
    end
  end

  class SingleEntry <R '/ entry / (\ d +)'
    def get (id)
      @entries = Entries.find_all_by_id (id)
      unless @ entries.nil?
        render: posts
      else
        @messages = ["That entry does not exist."]
        render: blank # I want to run Controllers :: ComplexQuery, instead of rendering a blank page.
      end
    end
  end
end
+2


source to share


2 answers


Something like that?

def get(id)
  @entries = Entries.find_all_by_id(id)
  unless @entries.nil?
    render :posts
  else
    r *MyApp.get(:ComplexQuery)
  end
end

      

See Camping.method_missing .



But I would also recommend moving ComplexQuery into a helper method:

module MyApp::Helpers
  def complex_query(conditions); end
end

      

Then you can in complex_query(something)

both SingleEntry and ComplexQuery.

+3


source


Try the following:



@entry = Entry.find_by_id(params[:id]) # returns nil when not found
if @entry.nil?
  flash[:notice] = "Entry not found"
  render :action => "recent" 
end

      

0


source







All Articles