控制器获取/创建/更新/销毁公用文件夹的文件

时间:2014-05-06 13:52:01

标签: ruby-on-rails ruby ruby-on-rails-4 routing

我正在使用Rails应用。 我正在尝试编写一个可以访问rails的Public文件夹文件的控制器。 我现在就被卡住了。

我尝试做的是在我的FileController上使用get方法。

这将获得一个变量" path"谁对应于要读取的文件路径谁位于rails的公共文件夹中。

我想修改routes.rb,以便在我发出" / file / path / to / htmlpage"的GET请求时请求向我发回文件的内容,就像HTML文件的字符串一样。

编辑:问题是需要这样的REST网址。

  • show => GET / file /:path
  • create => POST / file /:path
  • update => PUT / file /:path
  • destroy => DELETE / file /:path

所以我删除了旧路线并放了

resources :files

我的旧路线就像那样

  #get "file/:path" => 'file#get', as: :get
  #get "file/create/*path" => 'file#create', as: :create
  #post "file/create/*path/:content" => 'file#create', as: :create_content
  #get "file/update/*path/:content" => 'file#update', as: :update
  #get "file/destroy/:path" => 'file#destroy', as: :destroy

我的控制器可能有点奇怪,我需要一些建议。

class FileController < ApplicationController

  # get "file/:path"
  def show
    path = params[:path]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      @content = File.read( Rails.public_path.join( "#{path}.html" ) )
      puts @content
    else
      puts "The file you want to access doesn't exist"
    end
  end

  # get "file/create/:path"
  # post "file/create/*path/:content"
  def create
    path = params[:path]
    content = params[:content]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      puts "The file you want to create already exist"
    else
      File.write(Rails.public_path.join( "#{path}.html" ), "#{content}")
    end
  end

  # get "file/update/*path/:content"
  def update
    path = params[:path]
    content = params[:content]
    if File.exists?( Rails.public_path.join( "#{path}" ) )
      File.write(Rails.public_path.join( "#{path}" ), "#{content}")
    else
      puts "The file you want to update doesn't exist"
    end
  end

  # get "file/destroy/:path"
  def destroy
  path = params[:path]
  if File.exists?( Rails.public_path.join( "#{path}.html" ) )
    File.delete( Rails.public_path.join( "#{path}.html" ) )
  else
    puts "The file you want to delete doesn't exist"
  end
  end
end

但现在它无法正常工作,我认为我的路线和控制器存在问题,无法以正确的方式进行沟通。但我不明白我是怎么做到的。 我没有文件模型,因为我没有看到我想要做的事情。

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

试试这个:

# FileController

def action_name
  #check and filter here your path param, something like:
  path = params[:path].gsub /[^a-z0-9\.\/]+/, ''
  if File.exists?( Rails.root.join( "public/#{path}" ) )
    @content = File.read( Rails.root.join( "public/#{path}" ) )
  end
end

答案 1 :(得分:0)

您的问题有几个答案会产生不同的结果。

如果要在ruby中加载文件,可以引用公共路径,如:Rails.public_path。此外,您还可以通过Rails.root获取应用程序的根路径。这应该可以帮助您访问控制器中所需的文件。

但是我不得不问:你为什么要这样做?如果你只想提供文件,你可以让rails在默认情况下通过简单地提供链接本身的路径来执行此操作 - 它将提供公共文件。此外,您很可能根本不想通过ruby / rails推送它,并希望设置您的HTTP服务器为您提供静态文件,这将产生显着的性能优势。

答案 2 :(得分:0)

我会使用这样的模型:

class PublicFile
  attr_accessor :path, :content

  def save(params)
    File.write(params[:path], params[:content])
  end

  def destroy(params)
    File.delete(params[:path])
  end

  def initialize(params)
    @content = File.read(params[:path])
    @path    = params[:path]
  end

  def find(path_or_paths)
    # to implement
  end

  def all
    # to implement
  end

  def update_attributes(params)
    # to implement
  end
end

在控制器和视图中,您可以生成一个脚手架rails g scaffold PublicFile,您需要在运行脚手架之前移动模型,然后使用上面的操作文件的模型。

总之,我的意思是更改通常的Rails模型所具有的数据库,通过访问文件,这将在模型逻辑中完成。