Reputation: 3341
I'm working on a web application that has a view where data is fetched and parsed from a text file (the textfile is only available at the backend, not to the user). I've written a function that takes in the text file and converts it to an array of strings, it's called txt_to_arr
. Then I have another function line_fetcher
which just calls txt_to_arr
and outputs a random string from the array.
In my view, I call the controller's function as so: <% line_fetcher %>
.
I've put both txt_to_arr
and line_fetcher
into the view controller's helper rb
file, and when I run rails s
, the random string is not rendered at all. I've also tried <% puts line_fetcher %>
I've checked in Bash that the function does output random strings from the text file, so the function does work correctly. Also, the text file being parsed is in the public
folder. Does anyone have an idea why this might be?
Thanks a lot!
Upvotes: 1
Views: 2469
Reputation: 10251
In ERB: The <% %>
signify that there is Ruby code here to be interpreted. The <%= %>
says interpreted and output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
<%= line_fetcher %>
Upvotes: 2
Reputation: 3721
Simple erb
like <%= line_fetcher %>
would work good for simple variables.
But if you want output of any model/database instance then do:
<%= ModelName.first.inspect %>
Note the inspect
word.
And in case of using HAML
do:
=ModelName.first.inspect
Upvotes: 1
Reputation: 96454
Try placing the code in the controller and assigning the output to a variable using
a=`line_fetcher` (note the backtics) as detailed at
http://rubyquicktips.com/post/5862861056/execute-shell-commands
and then <%= a %>
in your view.
and place the file in the root of your rails app
Upvotes: 1
Reputation: 24337
Use <%= %>
to output something in your view, so:
<%= line_fetcher %>
Upvotes: 0