Praveen KJ
Praveen KJ

Reputation: 650

Rails passing variables from controller to a txt file and read using File.read

I am reading a txt file in ruby using File.read like below.

Let's say @name = tom

Text file contains.

Hello <%= @name %>

How can i pass @name to the text file and read it like below.

file = File.read('test.txt')

I want to pass rails instance variables to some places in txt file and read it so that file will be Hello Tom How can i achieve this? Any help will be appreciated. Thanks.

Upvotes: 0

Views: 354

Answers (1)

Nick Schwaderer
Nick Schwaderer

Reputation: 1028

  • Render your text file from your controller (send_file path_to_file)
  • Rename your text file to my_original_filename.txt.erb
  • Adding the erb suffix tells Rails to process the Ruby in the text file first (Via ERB).

Upvotes: 1

Related Questions