More idiomatic way of writing a filter

Is there a more elegant way of writing this?

 def verify_user
   @user = User.find_by_username params[:username]
   if @user.nil?
     message = xml.error do |xm|
       xm.message "User does not exists: #{params[:username]}"
     end
     render :xml => message
     return false
   else
     true
   end
 end

Thanks, Joel


http://wagerlabs.com/

I would do it like that…

def get_user
unless @user = User.find_by_username(name = params[:username])
render xml.error do |xm|
xm.message “User does not exists: #{name}”
end
end
@user
end

Joel R. schrieb:

yes, I you need :xml =>

I was sloppy :slight_smile:

Joel R. schrieb:

But then you need to write something like

render :xml => (xml.error do |xm|
xm.message …
end)

which looks a bit ugly with the parens in place. Any workaround apart
from stuffing the XML into a variable first?

Thanks, Joel


http://wagerlabs.com/

Cool! Wouldn’t you need to give render :xml => to make sure the
content type is set, though?

On Jun 30, 2006, at 1:22 PM, Pete wrote:

I would do it like that…

def get_user
unless @user = User.find_by_username(name = params[:username])
render xml.error do |xm|
xm.message “User does not exists: #{name}”
end
end
@user
end


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