Hi,
I would like to write form data into a file, and I would like
to know if there''s a way for me to use an ERB template
directly to output the file to disk?
I was going to use a series of puts("Full name: @order.full_name")
statements, but thought, there MUST be a better way!
any help or advice on what would be the most efficient way to
tackle this in RoR would be much appreciated.
ex ERB I used for generating the e-mail:
### order.rhtml
Full name: <%= @order.full_name %>
<%= @order.notes %>
<%= render(:partial => "./line_item", :collection =>
@order.line_items) %
__
Andrea
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Andrea B. wrote:> Hi, > > I would like to write form data into a file, and I would like > to know if there''s a way for me to use an ERB template > directly to output the file to disk? > > I was going to use a series of puts("Full name: @order.full_name") > statements, but thought, there MUST be a better way! > > any help or advice on what would be the most efficient way to > tackle this in RoR would be much appreciated. > > ex ERB I used for generating the e-mail: > > ### order.rhtml > Full name: <%= @order.full_name %> > <%= @order.notes %> > <%= render(:partial => "./line_item", :collection => > @order.line_items) %As I understand your question you have an ERB template and instead of rendering it to the screen you want to render the template to a file on the disk. One way of doing this is to use the render_to_string function from Rails. Check out: http://api.rubyonrails.com/classes/ActionController/Base.html#M000268 -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jacob,
Thank you very much for your help, this seems to be exactly what I
was
looking for! I just have one question about usage.
The documentation says:
"Renders according to the same rules as
render,
but returns the result in a string instead of
sending
it as the response body to the browser."
Does this mean it takes the ENTIRE ERB template (multiline) and
puts it into one long string? or does it convert each line of the
string into an item in a COLLECTION?
puts(template) # will this put all of it in?
or do I need to loop over and build the file one string at a time
for x in template
puts(x)
end
thanks again,
__
Andrea
On Jun 18, 11:35 am, Jacob Atzen
<j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote:
> As I understand your question you have an ERB template and instead of
> rendering it to the screen you want to render the template to a file on
> the disk. One way of doing this is to use the render_to_string function
> from Rails. Check out:
>
> http://api.rubyonrails.com/classes/ActionController/Base.html#M000268
>
> --
> Cheers,
> - Jacob Atzen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> Thank you very much for your help, this seems to be exactly what I > was > looking for! I just have one question about usage. > > The documentation says: > "Renders according to the same rules as > render, > but returns the result in a string instead of > sending > it as the response body to the browser." > > Does this mean it takes the ENTIRE ERB template (multiline) and > puts it into one long string? or does it convert each line of the > string into an item in a COLLECTION?string> > puts(template) # will this put all of it in? > > or do I need to loop over and build the file one string at a time > > for x in template > puts(x) > end > > > thanks again, > > __ > Andrea > > > > On Jun 18, 11:35 am, Jacob Atzen <j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote: > >> As I understand your question you have an ERB template and instead of >> rendering it to the screen you want to render the template to a file on >> the disk. One way of doing this is to use the render_to_string function >> from Rails. Check out: >> >> http://api.rubyonrails.com/classes/ActionController/Base.html#M000268 >> >> -- >> Cheers, >> - Jacob Atzen > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi,
Everything is in the ERB docs:
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html.
Small example from it:
require "erb"
# Create template.
template = %q{
From: James Edward Gray II
<james-AUi9nNu29NfWNcQ1/nO7itHuzzzSOjJt@public.gmane.org>
To: <%= to %>
Subject: Addressing Needs
<%= to[/\w+/] %>:
Just wanted to send a quick note assuring that your needs are
being
addressed.
I want you to know that my team will keep working on the issues,
especially:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
* <%= priority %>
% end
Thanks for your patience.
James Edward Gray II
}.gsub(/^ /, '''')
message = ERB.new(template, 0, "%<>")
# Set up template data.
to = "Community Spokesman <spokesman@ruby_community.org>"
priorities = [ "Run Ruby Quiz",
"Document Modules",
"Answer Questions on Ruby Talk" ]
# Produce result.
email = message.result
puts email
Generates:
From: James Edward Gray II
<james-AUi9nNu29NfWNcQ1/nO7itHuzzzSOjJt@public.gmane.org>
To: Community Spokesman <spokesman@ruby_community.org>
Subject: Addressing Needs
Community:
Just wanted to send a quick note assuring that your needs are being
addressed.
I want you to know that my team will keep working on the issues,
especially:
* Run Ruby Quiz
* Document Modules
* Answer Questions on Ruby Talk
Thanks for your patience.
James Edward Gray II
You may put your template in separate file to clean up code and use
some rhtml editor with syntax highlighting.
Wojtek
On Jun 18, 4:28 pm, "Andrea B."
<exheb...-XF9LSIGjJLGh6J55Ss3d3w@public.gmane.org>
wrote:> Hi,
>
> I would like to write form data into a file, and I would like
> to know if there''s a way for me to use an ERB template
> directly to output the file to disk?
>
> I was going to use a series of puts("Full name:
@order.full_name")
> statements, but thought, there MUST be a better way!
>
> any help or advice on what would be the most efficient way to
> tackle this in RoR would be much appreciated.
>
> ex ERB I used for generating the e-mail:
>
> ### order.rhtml
> Full name: <%= @order.full_name %>
> <%= @order.notes %>
> <%= render(:partial => "./line_item", :collection =>
> @order.line_items) %
>
> __
> Andrea
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---