Rails FileUtils.mkdir_p only creates parent directories

I have a controller in Rails with an action designed to create a new directory.

This action should create the directory "/ public / graph_templates / aaa / test". However, it leaves the final test directory. Why does this only create parent directories?

  def create_temporary_template
    dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test")
    FileUtils.mkdir_p dir
  end

Docs: http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p

+3
source share
2 answers

Since you are using dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test"),

that diris "#{Rails.root}/public/graph_templates/aaa".

You can simply pass the path to FileUtils.mkdir_p.

  def create_temporary_template
    dir = "#{Rails.root}/public/graph_templates/aaa/test"
    FileUtils.mkdir_p dir
  end
+5
source

dirname:

File.dirname("/foo/bar")
# => "/foo"

dirname . Per :

, , .

, :

File.dirname("/foo/bar/baz.txt")
# => "/foo/bar"

-.

Pathname, Ruby. File, Dir, FileUtils, FileTest , , , .

require 'pathname'
dir = Pathname.new("/foo/bar/baz.txt")
# => "/foo/bar"

dir.mkpath # would create the path

Pathname , .

+2

All Articles