简体   繁体   中英

Rails generate html file from html.erb template

I know thas it sounds a little weird. I need to generate an html file based on an html.erb template which has many variables. I will give an oversimplified example.

In my viwes/pages/home.html.erb I have:

<html>
     <h1> <%= @name %> </h1>
</html>

In my pages_controller.rb:

def home
   @name = 'Michaela'
end

When the home funcion is called, it will render the home.html.erb and in the browser, the content will be showed as if it was opnening an.html file that looks like the following:

<html>
    <h1> Michaela </h1>
</html>

What I need is to generate an plain html file with that content, after @name has been defined, so if instad of being hardcoded, it´s inputted by the user, the.html file defines it´s content accroding to that user input.

As Maxence recommended, I used the render_to_string helper to finally store the generated html into a variable, and manipulate it in that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM