Rails 5: How to add folders and files in application template script
I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.
In template.rb
require "fileutils"
require "shellwords"
def add_folders
mkdir views/components/buttons
mkdir csv/
end
def add_file
cd csv
touch user.csv
end
def add_readme
rm README.md
touch README.md
inject_into_file("README.md", "New readme..")
end
after_bundle do
add_folder
add_file
add_readme
end
But I don't know how to do it.
ruby-on-rails-5
add a comment |
I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.
In template.rb
require "fileutils"
require "shellwords"
def add_folders
mkdir views/components/buttons
mkdir csv/
end
def add_file
cd csv
touch user.csv
end
def add_readme
rm README.md
touch README.md
inject_into_file("README.md", "New readme..")
end
after_bundle do
add_folder
add_file
add_readme
end
But I don't know how to do it.
ruby-on-rails-5
add a comment |
I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.
In template.rb
require "fileutils"
require "shellwords"
def add_folders
mkdir views/components/buttons
mkdir csv/
end
def add_file
cd csv
touch user.csv
end
def add_readme
rm README.md
touch README.md
inject_into_file("README.md", "New readme..")
end
after_bundle do
add_folder
add_file
add_readme
end
But I don't know how to do it.
ruby-on-rails-5
I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.
In template.rb
require "fileutils"
require "shellwords"
def add_folders
mkdir views/components/buttons
mkdir csv/
end
def add_file
cd csv
touch user.csv
end
def add_readme
rm README.md
touch README.md
inject_into_file("README.md", "New readme..")
end
after_bundle do
add_folder
add_file
add_readme
end
But I don't know how to do it.
ruby-on-rails-5
ruby-on-rails-5
asked Nov 12 at 11:31
Jun Dalisay
828
828
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
FileUtils covers most of what you want. mkdir_p
uses the command line mkdir -p
command, which makes the full path if the directories don't exist.
IO.write
(which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.
Also, you'll want to make sure you use Rails.root.join
with your file paths. It's similar to File.join
, in that it helps you build a file path without doubling up your /
on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join
makes all of that safer.
Here's an example of using it on a unix system:
If Rails.root is '/some/cool/path/here'
, then Rails.root.join('views','components', 'buttons')
would be '/some/cool/path/here/views/components/buttons'
.
require 'fileutils'
require 'shellwords'
def add_folders
FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
FileUtils.mkdir_p(Rails.root.join('csv'))
end
def add_file
FileUtils.touch('Rails.root.join('csv', 'user.csv'))
end
def add_readme
File.write(Rails.root.join('README.md'), 'New readme..')
end
after_bundle do
add_folder
add_file
add_readme
end
Thanks, I usedFileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip theRails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always useRails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).
– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some moreRails.root.join
explanation.
– Nate
Nov 12 at 16:03
add a comment |
Check whether expected directory is present or not, then create it.
dir = "#Rails.root/public/folder_1"
FileUtils.mkdir(dir) unless File.directory? dir
Similarly, you can remove directory if present.
file = "#dir/alpha.png"
And you can further proceed with File IO in rails.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261274%2frails-5-how-to-add-folders-and-files-in-application-template-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
FileUtils covers most of what you want. mkdir_p
uses the command line mkdir -p
command, which makes the full path if the directories don't exist.
IO.write
(which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.
Also, you'll want to make sure you use Rails.root.join
with your file paths. It's similar to File.join
, in that it helps you build a file path without doubling up your /
on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join
makes all of that safer.
Here's an example of using it on a unix system:
If Rails.root is '/some/cool/path/here'
, then Rails.root.join('views','components', 'buttons')
would be '/some/cool/path/here/views/components/buttons'
.
require 'fileutils'
require 'shellwords'
def add_folders
FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
FileUtils.mkdir_p(Rails.root.join('csv'))
end
def add_file
FileUtils.touch('Rails.root.join('csv', 'user.csv'))
end
def add_readme
File.write(Rails.root.join('README.md'), 'New readme..')
end
after_bundle do
add_folder
add_file
add_readme
end
Thanks, I usedFileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip theRails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always useRails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).
– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some moreRails.root.join
explanation.
– Nate
Nov 12 at 16:03
add a comment |
FileUtils covers most of what you want. mkdir_p
uses the command line mkdir -p
command, which makes the full path if the directories don't exist.
IO.write
(which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.
Also, you'll want to make sure you use Rails.root.join
with your file paths. It's similar to File.join
, in that it helps you build a file path without doubling up your /
on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join
makes all of that safer.
Here's an example of using it on a unix system:
If Rails.root is '/some/cool/path/here'
, then Rails.root.join('views','components', 'buttons')
would be '/some/cool/path/here/views/components/buttons'
.
require 'fileutils'
require 'shellwords'
def add_folders
FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
FileUtils.mkdir_p(Rails.root.join('csv'))
end
def add_file
FileUtils.touch('Rails.root.join('csv', 'user.csv'))
end
def add_readme
File.write(Rails.root.join('README.md'), 'New readme..')
end
after_bundle do
add_folder
add_file
add_readme
end
Thanks, I usedFileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip theRails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always useRails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).
– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some moreRails.root.join
explanation.
– Nate
Nov 12 at 16:03
add a comment |
FileUtils covers most of what you want. mkdir_p
uses the command line mkdir -p
command, which makes the full path if the directories don't exist.
IO.write
(which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.
Also, you'll want to make sure you use Rails.root.join
with your file paths. It's similar to File.join
, in that it helps you build a file path without doubling up your /
on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join
makes all of that safer.
Here's an example of using it on a unix system:
If Rails.root is '/some/cool/path/here'
, then Rails.root.join('views','components', 'buttons')
would be '/some/cool/path/here/views/components/buttons'
.
require 'fileutils'
require 'shellwords'
def add_folders
FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
FileUtils.mkdir_p(Rails.root.join('csv'))
end
def add_file
FileUtils.touch('Rails.root.join('csv', 'user.csv'))
end
def add_readme
File.write(Rails.root.join('README.md'), 'New readme..')
end
after_bundle do
add_folder
add_file
add_readme
end
FileUtils covers most of what you want. mkdir_p
uses the command line mkdir -p
command, which makes the full path if the directories don't exist.
IO.write
(which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.
Also, you'll want to make sure you use Rails.root.join
with your file paths. It's similar to File.join
, in that it helps you build a file path without doubling up your /
on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join
makes all of that safer.
Here's an example of using it on a unix system:
If Rails.root is '/some/cool/path/here'
, then Rails.root.join('views','components', 'buttons')
would be '/some/cool/path/here/views/components/buttons'
.
require 'fileutils'
require 'shellwords'
def add_folders
FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
FileUtils.mkdir_p(Rails.root.join('csv'))
end
def add_file
FileUtils.touch('Rails.root.join('csv', 'user.csv'))
end
def add_readme
File.write(Rails.root.join('README.md'), 'New readme..')
end
after_bundle do
add_folder
add_file
add_readme
end
edited Nov 12 at 16:03
answered Nov 12 at 13:53
Nate
1,118111
1,118111
Thanks, I usedFileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip theRails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always useRails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).
– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some moreRails.root.join
explanation.
– Nate
Nov 12 at 16:03
add a comment |
Thanks, I usedFileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip theRails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always useRails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).
– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some moreRails.root.join
explanation.
– Nate
Nov 12 at 16:03
Thanks, I used
FileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html– Jun Dalisay
Nov 12 at 15:56
Thanks, I used
FileUtils.mkdir_p 'csv'
following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html– Jun Dalisay
Nov 12 at 15:56
@JunDalisay Be careful with that. You could end up in a bad place if you skip the
Rails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).– Nate
Nov 12 at 15:59
@JunDalisay Be careful with that. You could end up in a bad place if you skip the
Rails.root.join
part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join
and then use file paths that are relative to the root of your Rails app (like I showed).– Nate
Nov 12 at 15:59
@JunDalisay I just updated my answer with some more
Rails.root.join
explanation.– Nate
Nov 12 at 16:03
@JunDalisay I just updated my answer with some more
Rails.root.join
explanation.– Nate
Nov 12 at 16:03
add a comment |
Check whether expected directory is present or not, then create it.
dir = "#Rails.root/public/folder_1"
FileUtils.mkdir(dir) unless File.directory? dir
Similarly, you can remove directory if present.
file = "#dir/alpha.png"
And you can further proceed with File IO in rails.
add a comment |
Check whether expected directory is present or not, then create it.
dir = "#Rails.root/public/folder_1"
FileUtils.mkdir(dir) unless File.directory? dir
Similarly, you can remove directory if present.
file = "#dir/alpha.png"
And you can further proceed with File IO in rails.
add a comment |
Check whether expected directory is present or not, then create it.
dir = "#Rails.root/public/folder_1"
FileUtils.mkdir(dir) unless File.directory? dir
Similarly, you can remove directory if present.
file = "#dir/alpha.png"
And you can further proceed with File IO in rails.
Check whether expected directory is present or not, then create it.
dir = "#Rails.root/public/folder_1"
FileUtils.mkdir(dir) unless File.directory? dir
Similarly, you can remove directory if present.
file = "#dir/alpha.png"
And you can further proceed with File IO in rails.
answered Nov 12 at 13:20
ray
79412
79412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261274%2frails-5-how-to-add-folders-and-files-in-application-template-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown