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 Reymont schrieb:> 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/ > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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/
yes, I you need :xml => I was sloppy :-) Joel Reymont schrieb:> 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/ > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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/