Rails 3 displays a plain text page using a view

I have a Linux configuration file stored in my database in Rails and I would like to be able to load the configuration through a web request

My goal on the Linux side is to twist / wget the webpage, compare it with the current configuration, and then hup on the server. This is easy enough to do in a script.

Under normal Rails conditions you can do

render :text => @config_file 

However, I need to format the data first to apply static headers, etc. This is not a single line, so I need to be able to display the view.

I have the following set in my controller, but I still get the minimum set of HTML tags in the document

 render(:content_type => 'text/plain', :layout => false); 

I did something similar in .Net before, so it printed a text file with \n interpreted. How to get it in Rails?

+1
source share
1 answer

This is usually done using

 # config/initializers/mime_types.rb # ... # Mime::Type.register "text/plain", :plaintext # No changes needed as rails comes preconfigured with the text/plain mime type # app/controllers/my_controller.rb class MyController < ApplicationController def my_action respond_to do |format| format.text end end end 

and view file

 # app/views/my_controller/my_action.text.erb ... 

About the minimal HTML you'll find in the DOM: do you see this inside some kind of inspector in a browser, such as those included in Google Chrome or safari? If so, don’t worry, this is added by the browser to display the embedded text / plain document. If you look at the source of the delivered document (ctrl-u), HTML should not appear.

+8
source

All Articles