简体   繁体   English

Ruby on Rails目录浏览器

[英]ruby on rails directory browser

I'm trying to write a simple RoR app that lists all directories in a specific path then allows me to click that file or directory to either open the file or open the directory. 我正在尝试编写一个简单的RoR应用程序,该应用程序列出特定路径中的所有目录,然后允许我单击该文件或目录以打开文件或打开目录。 I'm using the following code which works to list the directories but I can't figure out how to open the file or directory from here. 我正在使用下面的代码来列出目录,但是我不知道如何从此处打开文件或目录。 When clicking the directory I get a "No route matches [GET] "/selected file" error. 单击目录时,出现“没有路由与[GET]匹配/所选文件”错误。

controller
    @file = Dir.foreach ("/specifiedpath/")

view
<% @file.each do |file| %> 
    <tr>
        <td><%= link_to file, file %></td>
    </tr>
<% end %> 

Any help would be appreciated! 任何帮助,将不胜感激!

You need something in your routes.rb that looks like this: 您在routes.rb中需要如下所示的内容:

get '/browse/*path' => 'browse#show', as: :browse

In your browse_controller , you'll want a show action that does something like: 在您的browse_controller ,您需要执行以下操作的show动作:

class BrowseController < ApplicationController

  def show
    @path = params[:path] || '/some/default'
    @files = Dir.foreach(path)
  end

Finally, in your browse/show.html.haml (I prefer HAML, sorry) view, something like: 最后,在您的browse/show.html.haml (抱歉,我更喜欢HAML)视图中,类似于:

- @files.each do |file|
  %tr
    %td= link_to file, browse_path(File.join(@path, file))

(Pardon me if it all doesn't work outright—haven't time to fully test a fleshed out solution right now—but I can delve into it a little more if you have trouble getting it to work yourself.) (请原谅我,如果这一切都无法正常工作-现在还没有时间完全测试一个完善的解决方案-但如果您无法自行解决问题,我可以进一步研究。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM