Rhomobile:读取txt文件并显示内容Ruby

时间:2013-06-06 08:14:02

标签: ruby rhomobile rhodes

我想在我的Rhomobile应用程序中打开,阅读并显示一个txt文件。 首先,我想显示文件的完整内容。但这是我的问题。

def text
fileName = File.join(Rho::RhoApplication::get_base_app_path(), '/app/test.txt')
    f = File.open(fileName, 'r')
    while line = f.gets
    puts line
    end
  f.close
    redirect :action => :index  
    end

代码读取txt文件,但是如何调用方法并在页面上显示结果。 我不知道什么是可变的?

请帮帮我:)非常感谢!

1 个答案:

答案 0 :(得分:0)

如果您只想阅读整个文件,可以使用以下方法:

@file_content = File.read(fileName);

您可以将文件的内容分配给实例变量(以@为前缀)并在视图中显示(*.html.erb*.erb):

File content: <%= @file_content %>

此外,如果您致电redirect,则系统不会呈现您的视图,系统会将用户重定向到指定的操作(index)。所以控制器动作的代码应该是这样的:

def text
  @file_content = File.read(fileName);
  render action: 'text'
end
相关问题