More idiomatic way of doing this

Folks,

I’m trying to write a validating filter that checks if the user
exists and renders XML if not. Most of the controllers will be
returning XML on some error or other so I’m including RenderError in
ApplicationController.

Is my approach a sound one? Also, is there a shorter and more
idiomatic way of writing this:

   if @user.nil?
     render_user_exists params[:username]
     false
   else
     true
   end

Rest of the code below…

lib/util/render_error.rb:

module RenderError

def render_user_exists data
render :partial => “shared/error”,
:system => “udb”,
:code => 201,
:fatal => true,
:message => “user exists”,
:property => “username”,
:baddata => data
end

end

app/views/shared/_error.rxml:

xml.error system => @system, code => @code, fatal => @fatal do
xml.message @message
xml.property @property
xml.baddata @baddata
end

app/controllers/application.rb:

require ‘util/render_error’

class ApplicationController < ActionController::Base

include RenderError

end

app/controllers/user_controller.rb:

class UserController < ApplicationController

before_filter :verify_user

private

 def verify_user
   @user = User.find_by_username params[:username]
   if @user.nil?
     render_user_exists params[:username]
     false
   else
     true
   end
 end

end

Thanks, Joel


http://wagerlabs.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs