可以在开发记录器中打印一行吗?

时间:2011-01-25 07:03:31

标签: ruby-on-rails debugging

我有一些类似于这个users_controller.rb的代码,我需要在development.log中打印第30行

line#29  def selectrole
line#30    @userrole = RolesUser.find(:all, :conditions =>["p.user_id = ? and p.status = ? ",session[:user_id], params['status']])
line#31    logger.debug print_line(30)
line#32  end

我可以在我的development.log中看到第30行

@userrole = RoleUser.find(:all, :conditions => ["p.user_id = ? and p.status = ? ", 1234, 'Active'])

编写“print_line”函数的方法是什么?这是我的print_line代码?

def print_line(file_name, line) 
   counter = 1
   printline = "-------- NO SUCH LINE --------"
   File.open(file_name, "r") do |infile|
      while (line_text = infile.gets)
         if counter == line
            printline =  "#{counter} :: " +  line_text    
            break
         end 
         counter += 1
      end
   end
   printline
end

从这个功能我得到这样的

 @userrole = RolesUser.find(:all, :conditions =>["p.user_id = ? and p.status = ? ",session[:user_id], params['status']])

他们以任何方式找到并用各自的值替换变量吗?

1 个答案:

答案 0 :(得分:3)

假设您主要想知道:conditions的内容,为什么不做这样的事情:

def selectrole
  conditions = ["p.user_id = ? and p.status = ? ",session[:user_id], params['status']]
  logger.debug(conditions)
  @userrole = RolesUser.find(:all, :conditions => conditions)
end